Amritha
Amritha

Reputation: 51

Unable to connect to remote server while sending email in asp.net

The above error arising while using email sending code my code is

string fromAddress = "mymail";
string fromPassword = "mypassword";
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
smtp.Send(fromAddress, toAddress, MailSubject, Body);

I googled many times but didnt get proper solution. Port 587 is enbled and firewall blocking also not there.

Upvotes: 1

Views: 11433

Answers (4)

Amritha
Amritha

Reputation: 51

Thanks for the response.I got solution as if you are using any antivirus software check it's log to see whether it is because of the antivirus. I faced same problem when McAffee was blocking my mails (there is a security policy - Prevent mass mailing worms from sending mails). Edit this policy and add your application to the exception list. In my case this sorted the problem. Please check if it works for you.

Upvotes: 1

4b0
4b0

Reputation: 22323

This code work for me.Try this.

  MailMessage mM = new MailMessage();
        mM.From = new MailAddress("[email protected]");
        mM.To.Add(Email);
        mM.Subject = "Your Sub";
        mM.Body = "Your Body" ;
        mM.IsBodyHtml = true;
        mM.Priority = MailPriority.High;
        SmtpClient sC = new SmtpClient("smtp.gmail.com");
        sC.Port = 587;
        sC.Credentials = new NetworkCredential("YourGmail", "YourPassword");
        //sC.EnableSsl = true;
        sC.EnableSsl = true;
        sC.Send(mM);

Upvotes: 1

highwingers
highwingers

Reputation: 1669

I use following code for Gmail:

Function SendMail_Gmail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String) As Boolean

        Dim mailmsg As New System.Net.Mail.MailMessage()    
        mailmsg.From = New MailAddress(strFrom)    
        mailmsg.To.Add(strTo)    
        mailmsg.Subject = strSubject    
        mailmsg.IsBodyHtml = True    
        mailmsg.Body = strBody    
        mailmsg.Priority = System.Net.Mail.MailPriority.Normal

        Dim client As New System.Net.Mail.SmtpClient()

        client.Host = "smtp.gmail.com"
        client.Port = "587"    
        client.Credentials = New System.Net.NetworkCredential("[email protected]", "Yourpassword")

       client.EnableSsl = True

        Dim userstate As Object = mailmsg   


        client.Send(mailmsg)
        Return True
    End Function

Upvotes: 0

Priyank Patel
Priyank Patel

Reputation: 6996

try
   {
      MailMessage mail = new MailMessage();
      mail.To.Add("sender id");
      mail.From = new MailAddress("your id");   
      mail.Subject = "Mail from my web page";
      mail.Body ="Body Content";
      mail.IsBodyHtml = true;
      SmtpClient smtp = new SmtpClient();
      smtp.Host = "smtp.gmail.com";
      //Or your Smtp Email ID and Password  
      smtp.Credentials = new System.Net.NetworkCredential
      ("XYZ", "XXXXX");
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
      smtp.EnableSsl = true;
      smtp.Send(mail);
   }


catch (Exception ex)
    {
        //display exception             

    }

Upvotes: 1

Related Questions