I am trying to send an email using the C# SmtpClient class. The "catch" block gets executed after it attempts to "send" the message. When I go to look at the exception, the InnerException says "Unable to connect to the remote server" and the message says "Failure sending mail". The StatusCode is "General failure".
Am I missing certain credentials? Do I need to set some more fields to the instance of the SmtpClient or MailMessage?
Here is the code:
private void sendEmail(string recepientEmailAddress) {
string subjectEmail = "Subject Email";
MailAddress to = new MailAddress("[email protected]");
MailAddress from = new MailAddress("[email protected]");
MailMessage message = new MailMessage(from, to);
message.Subject = subjectEmail;
message.IsBodyHtml = true;
message.Body = "Body example";
try
{
SmtpClient sc = new SmtpClient(ConfigurationManager.AppSettings["MailServer"].ToString());
sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.EnableSsl = true;
sc.UseDefaultCredentials = true;
sc.Send(message);
}
catch (Exception e)
{
Helpers.ErrorLogger.ProcessError(e.Message, e.StackTrace, "RegistrationController", "SendEmail");
}
}
MailServerspecified in theAppSettingsis not valid. - user700390