You must Sign In to post a response.
  • Category: .NET

    Service unavailable error while sending mail

    Hi,
    I am sending mails through mvc application.before some days ago it was working fine.but suddenly iti is thowing error "System.net.mail.smtpexception service not available closing transmission channel".i dont know why this is happening.it doen't shows any error in my code while debugging.code is as follows,
    public static bool SendEmail(Email EmailObject,string Displayname)
    {

    string EmailFrom = ConfigurationMessage.Configuration_Mail_MailUsername; //objCache.GetConfigValues()[CacheItems.ConfigKeys.MailUsername.ToString()];
    EmailObject.EmailFrom = ConfigurationMessage.Configuration_Mail_MailFrom; //objCache.GetConfigValues()[CacheItems.ConfigKeys.MailFrom.ToString()];
    string Password = ConfigurationMessage.Configuration_Mail_MailPassword; //objCache.GetConfigValues()[CacheItems.ConfigKeys.MailPassword.ToString()];

    // EmailObject.EmailTo = ConfigurationMessage.Configuration_Mail_MailTo;
    SmtpClient client = new SmtpClient();
    if (!string.IsNullOrEmpty(ConfigurationMessage.Configuration_Mail_SmtpServerPort))
    {
    client.Port = int.Parse(ConfigurationMessage.Configuration_Mail_SmtpServerPort); //int.Parse(objCache.GetConfigValues()[CacheItems.ConfigKeys.SmtpServerPort.ToString()]);
    }
    client.Host = ConfigurationMessage.Configuration_Mail_SmtpServer; //objCache.GetConfigValues()[CacheItems.ConfigKeys.SmtpServer.ToString()];
    client.EnableSsl = ConfigurationMessage.Configuration_Mail_EnableSsl;

    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(EmailFrom, Password);

    MailMessage mm = new MailMessage(EmailObject.EmailFrom, EmailObject.EmailTo, EmailObject.EmailSubject, EmailObject.EmailBody);
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.IsBodyHtml = true;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    mm.From = new MailAddress(ConfigurationMessage.Configuration_Mail_MailFrom, ConfigurationMessage.Configuration_Mail_MailFromDisplayName);

    client.Send(mm);
    }

    any solution??
  • #765534
    Disposing the smtpclient (client) did the trick.
    I like wrapping it in a using block. That'll force the dispose and it's very elegant.

    using(SmtpClient client = new SmtpClient())
    {
    if (!string.IsNullOrEmpty(ConfigurationMessage.Configuration_Mail_SmtpServerPort))
    {
    client.Port = int.Parse(ConfigurationMessage.Configuration_Mail_SmtpServerPort); //int.Parse(objCache.GetConfigValues()[CacheItems.ConfigKeys.SmtpServerPort.ToString()]);
    }
    client.Host = ConfigurationMessage.Configuration_Mail_SmtpServer; //objCache.GetConfigValues()[CacheItems.ConfigKeys.SmtpServer.ToString()];
    client.EnableSsl = ConfigurationMessage.Configuration_Mail_EnableSsl;

    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(EmailFrom, Password);

    MailMessage mm = new MailMessage(EmailObject.EmailFrom, EmailObject.EmailTo, EmailObject.EmailSubject, EmailObject.EmailBody);
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.IsBodyHtml = true;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    mm.From = new MailAddress(ConfigurationMessage.Configuration_Mail_MailFrom, ConfigurationMessage.Configuration_Mail_MailFromDisplayName);

    client.Send(mm);
    }

  • #765537
    Thanks for Reply.

  • #765749
    This error means that the SMTP server is not responsing. Please make sure that the SMTP server settings are configured properly in the application or try disposing the smtpclient object.
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net


Sign In to post your comments