1

I am trying the send email code in asp.net mvc but I keep getting the error {"Failure sending mail."} I have referred to many of the questions asked here and tried out the suggested sollutions but still got the same error. Where am I going wrong?
Here is my code:

 private static void SendEmailWithErrors(string result)
    {
        SmtpClient smtpclient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            MailAddress fromAddress = new MailAddress("email address");
            smtpclient.Host = "smtp.gmail.com";
            smtpclient.Port = 587;
            smtpclient.UseDefaultCredentials = true;
            smtpclient.EnableSsl = true;
            smtpclient.Credentials = new System.Net.NetworkCredential("email address", "password");
            message.From = fromAddress;
            message.To.Add("email address");
            message.Subject = "Exception raised";
            message.IsBodyHtml = false;
            message.Body = result;
            smtpclient.Send(message);
        }
        catch(Exception ex)
        {

        }
    }
Codor
  • 17,447
  • 9
  • 29
  • 56
Sumedha Vangury
  • 643
  • 2
  • 17
  • 43
  • Failure sending mail is as generic as they get. Can you post a stack trace? I'm assuming it's exceptioning out, so what does your debugger say? – JNYRanger Mar 10 '15 at 13:08
  • What is the exception? Can you post a stack trace? – nxu Mar 10 '15 at 13:09
  • Check the exception as suggested by @nXu, you might be under a firewall and it might prevent you from accessing mail servers – Carbine Mar 10 '15 at 13:12
  • You could try ** EnableSsl=false** ; It solved my problem . – katmanco Mar 10 '15 at 13:13
  • Gmail requires SSL (TLS), don't disable it. Check this out btw: http://stackoverflow.com/questions/9801224/smtpclient-with-gmail – nxu Mar 10 '15 at 13:19
  • remove the line "smtpclient.UseDefaultCredentials = true;" since he uses the AppPool default credentials instead of the ones you are setting. – Paulo Correia Mar 10 '15 at 14:16

3 Answers3

0

Check in to your Email ID from which you are sending the message from. There is a chance there is a warning mail sent to you that someone is trying to illegally access your mail. Just make sure to lower your security for your email to allow 3rd party apps to access it. In case of Gmail, this is done in privacy settings. It should work fine once the security of the from email address is lowered.

SanyTiger
  • 666
  • 1
  • 8
  • 23
0
private static void SendEmail(string result)
{ 

  var fromAddress = new MailAddress("from@gmail.com", "From Name");
  var toAddress = new MailAddress("to@example.com", "To Name");

  var smtp = new SmtpClient
  {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  };
  using (var message = new MailMessage(fromAddress, toAddress)
   {
    Subject = "Mail Subject",
    Body = result 
   })
   {
    smtp.Send(message);
   }
}
Codemasta
  • 111
  • 4
0

Thank you all for posting your answers, I am using the local machine and the IP address is already configured in the server machine, so that was causing the conflict. The code works fine when I run the application from the server machine.

Sumedha Vangury
  • 643
  • 2
  • 17
  • 43