tags:

views:

105

answers:

2

I have a web application I set it to send email automatically by gmail account using MailHelper.cs and web.config setting. my application have been working for 10 days and now it gives me error and stop during sending email I handle that error it was "Failure sending mail." and the inner-exception is "A socket operation was attempted to an unreachable host 216.239.59.109:587" here is code :

<mailSettings>
  <smtp from="[email protected]">
    <network host="smtp.gmail.com"
             port="587"
             userName="[email protected]"
             password="mypass"/>
  </smtp>
</mailSettings>

using System.Net.Mail;

public class MailHelper
{
   /// <summary>
   /// Sends an mail message
   /// </summary>
   /// <param name="from">Sender address</param>
   /// <param name="to">Recepient address</param>
   /// <param name="bcc">Bcc recepient</param>
   /// <param name="cc">Cc recepient</param>
   /// <param name="subject">Subject of mail message</param>
   /// <param name="body">Body of mail message</param>
   public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
   {
      // Instantiate a new instance of MailMessage
      MailMessage mMailMessage = new MailMessage();

      // Set the sender address of the mail message
      mMailMessage.From = new MailAddress(from);
      // Set the recepient address of the mail message
      mMailMessage.To.Add(new MailAddress(to)); 
      // Check if the bcc value is null or an empty string
      if ((bcc != null) && (bcc != string.Empty))
      {
         // Set the Bcc address of the mail message
         mMailMessage.Bcc.Add(new MailAddress(bcc));
      }

      // Check if the cc value is null or an empty value
      if ((cc != null) && (cc != string.Empty))
      {
         // Set the CC address of the mail message
         mMailMessage.CC.Add(new MailAddress(cc));
      }       // Set the subject of the mail message
      mMailMessage.Subject = subject;
      // Set the body of the mail message
      mMailMessage.Body = body; 
      // Set the format of the mail message body as HTML
      mMailMessage.IsBodyHtml = true;
      // Set the priority of the mail message to normal
      mMailMessage.Priority = MailPriority.Normal;

      // Instantiate a new instance of SmtpClient
            SmtpClient mSmtpClient = new SmtpClient();
            mSmtpClient.Host = "smtp.gmail.com";
            mSmtpClient.Port = 587;
            mSmtpClient.EnableSsl = true;

      // Send the mail message
      mSmtpClient.Send(mMailMessage);
   }
}
A: 

I would highly recommend installing Elmah (or turning on full exception information in the config file) so that you can capture the exception in your ASP.NET application. Once you do that, you should be able to give more information about the exception which you can then replicate here.

The exception (and I'm guessing the InnerException property more importantly here) probably will tell you everything that you (or we) need.


Based on the comment, it would seem that you have having issues on the host with allowing you to reach that address (or the endpoint is rejecting you). It seems more a config/host issue than an issue with the library.

Can you get it to work from a console app on your machine?

casperOne
Inner Exception is : A socket operation was attempted to an unreachable host 216.239.59.109:587
kamiar3001
Yes, I can run it easly from my local machin but when I published it into my host server it gives me error like this.
kamiar3001
A: 

As stated in casperOne's edit, this doesn't appear to be a library issue but a host/gMail server issue.

One of the sides of the equation is preventing the communication. I would tend to point the finger at the host and not gMail.

How many emails were you able to send before this started acting up? Who is your host?

Many host have a limit on the number of outgoing emails they will let you send, as they don't want you to make them look like a spammer.

Clarence Klopfstein
Yes, I think so but I don't have any limitation for outgoing mail I think I should talk with my hoster.
kamiar3001
Thanks, It is the correct answer.
kamiar3001