I m trying to send an email with below code it run correctly and email sent on live server but when i try to send an email using locally with the same code, the email is not getting sent.
It gives error "failure sending mail".
smtp.office365.com
port=587
public void SendMail(string recipient, string subject, string body, string AttachmentFiles = null, string cc = "", string bcc = "")
{
//SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SMTP_SERVER"].ToString());
client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SMTP_PORT"].ToString());
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["NetworkCredential_UserId"].ToString(), ConfigurationManager.AppSettings["NetworkCredential_Password"].ToString());
client.EnableSsl = true;
client.Credentials = credentials;
try
{
var mail = new MailMessage();
string from = "";
from = ConfigurationManager.AppSettings["NetworkCredential_UserId"].ToString();
if (!string.IsNullOrEmpty(from.Trim()))
{
mail.From = new MailAddress(from);
}
if (!string.IsNullOrEmpty(recipient.Trim()))
{
foreach (var address in recipient.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
mail.To.Add(address);
}
//mail.To.Add(recipient);
}
if (!string.IsNullOrEmpty(cc.Trim()))
{
foreach (var ccaddress in cc.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
mail.CC.Add(ccaddress);
}
//mail.CC.Add(cc);
}
if (!string.IsNullOrEmpty(bcc.Trim()))
{
foreach (var bccaddress in bcc.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
mail.Bcc.Add(bccaddress);
}
//mail.Bcc.Add(bcc);
}
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
if (!string.IsNullOrEmpty(AttachmentFiles))
{
foreach (string a in AttachmentFiles.Split(new char[] { ',' }))
{
if (!string.IsNullOrEmpty(a))
{
Attachment att = new Attachment(a);
mail.Attachments.Add(att);
}
}
}
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}