office365 SMTP Error: Failure sending mail in C#

Last Reply one month ago By dharmendr

Posted one month ago

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;
            }
        }

 

Posted one month ago

Use smtp-mail.outlook.com as host and Port as 587 or 25. It will work.


Posted one month ago

Try to use port 25. Port 587 might be blocked.

Also try to change the host with smtp-mail.outlook.com as mentioned by rani and checked.