0

Possible Duplicate:
No connection could be made because the target machine actively refused it?

I Face Critical Problem During Sending Mail in Asp.net please tell me Solution if Anyone know About this. This Is My Code for Sending Mail

MailMessage mm = new MailMessage();

        SmtpClient smtp = new SmtpClient();

        mm.From = new MailAddress(txtfrom.Text);

        mm.To.Add(new MailAddress(txtto.Text));



        mm.Subject = txtsubject.Text;

        mm.Body = txtmessage.Text;

        mm.IsBodyHtml = true;

        smtp.Host = "smtp.gmail.com"; //You can add this in the webconfig

        smtp.EnableSsl = true;

        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();

        NetworkCred.UserName = "mymail@gmail.com";

        NetworkCred.Password = "my password";
        smtp.UseDefaultCredentials = true;

        smtp.Credentials = NetworkCred;

        smtp.Port = 587; //this is Gmail port for e-mail

        smtp.Send(mm);//send an e-mail

The Problem is That when i Click on Send Button it Show following Error

"No connection could be made because the target machine actively refused it 173.194.79.109:587"

Please tell me Solution ..

Community
  • 1
  • 1
  • Why are you setting UseDefaultCredentials to true? This will try to send the credentials of the currently logged on user, not the ones you have initialised - set this to false and retry. – Kevin Main May 15 '12 at 09:44
  • ref [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) and set `UseDefaultCredentials` to `false` as you are specifying your custom credentials – Niranjan Singh May 15 '12 at 09:50

5 Answers5

0

you need to set false the UseDefaultCredentials property.

smtp.UseDefaultCredentials = false;
Adeel
  • 19,075
  • 4
  • 46
  • 60
0

That error message usually means a firewall is blocking your connection. Instead of using your machine name "173.194.79.109:587", try using "localhost".

coder
  • 13,002
  • 31
  • 112
  • 214
0

This is how I use it.

SmtpClient gglClient = new SmtpClient("173.194.67.108", 587);
gglClient.UseDefaultCredentials = false;
gglClient.DeliveryMethod = SmtpDeliveryMethod.Network;
gglClient.Credentials = new NetworkCredential("username@gmail.com", "password");
gglClient.EnableSsl = true;

MailMessage msg = new MailMessage("", email);
msg.Subject = "";
msg.Body = @" ";

msg.IsBodyHtml = false;
msg.BodyEncoding = UTF8Encoding.UTF8;

gglClient.Timeout = 8000;
ServicePointManager.ServerCertificateValidationCallback =
    delegate(object s, X509Certificate certificate, X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; };

gglClient.Send(msg);
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
bresleveloper
  • 5,940
  • 3
  • 33
  • 47
0
    protected void SendMail_Click(object sender, EventArgs e)
    {
        var fromAddress = new MailAddress(fromid.Text, fromname.Text);
        var toAddress = new MailAddress(toid.Text, toname.Text);
        string fromPassword = pswd.Text;
        string subject = subjectbox.Text;
         string body = bodybox.Text;

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body,
            IsBodyHtml = false

        })
        {
            smtp.Send(message);
        }

    }
Syed Yunus
  • 198
  • 3
  • 4
  • 14
0

This is the function which i checked to send mail...and it's working properly.

`

    private static bool testsendemail(MailMessage messageBody)
    {

        try

        {

        MailMessage message1 = new MailMessage();

        SmtpClient smtpClient = new SmtpClient();

        string msg = string.Empty;

        MailAddress fromAddress = new MailAddress("FromMail@Test.com");
        message1.From = fromAddress;
        message1.To.Add("ToMail@Test1.com");
        message1.Subject = "This is Test mail";
        message1.IsBodyHtml = true;
        message1.Body ="You can write your body here"+messageBody;
        smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
        smtpClient.Port = 587;
        smtpClient.EnableSsl = false;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new  System.Net.NetworkCredential("SenderMail@yahoo.com", "YourPassword");

        smtpClient.Send(message1);
    }
    catch
    {
        return false;
    }
    return true;

}`           

Thank You.

Chetan S
  • 935
  • 1
  • 11
  • 21