简体   繁体   中英

How to send email from outlook using asp.net core 1.1

can any one help me how to send email from asp.net core application from outlook. I'm able to send emails from gmail using MailKit but its failing with outlook

Following is the code i'm using

            string FromAddress = "fromemailadress";
            string FromAdressTitle = "Email from ASP.NET Core 1.1";
            //To Address  
            string ToAddress = "Toemailadress";
            string ToAdressTitle = "Microsoft ASP.NET Core";
            string Subject = "Hello World - Sending email using ASP.NET Core 1.1";
            string BodyContent = "ASP.NET Core was previously called ASP.NET 5. It was renamed in January 2016. It supports cross-platform frameworks ( Windows, Linux, Mac ) for building modern cloud-based internet-connected applications like IOT, web apps, and mobile back-end.";

            //Smtp Server  
            string SmtpServer = "smtp.live.com";
            //Smtp Port Number  
            int SmtpPortNumber = 587;

            var mimeMessage = new MimeMessage();
            mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
            mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
            mimeMessage.Subject = Subject;
            //mimeMessage.Body = new TextPart("plain")
            //{
            //    Text = BodyContent

            //};
            var bodyBuilder = new BodyBuilder();
            bodyBuilder.HtmlBody = @"<b style='color:blue'>This is bold and this is <i>italic</i></b>";
            mimeMessage.Body = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient())
            {

                client.Connect(SmtpServer, SmtpPortNumber, false);
                // Note: only needed if the SMTP server requires authentication  
                // Error 5.5.1 Authentication   
                client.Authenticate("Email", "Password");
                client.Send(mimeMessage);
                Console.WriteLine("The mail has been sent successfully !!");
                Console.ReadLine();
                client.Disconnect(true);

            }

its throwing error smptp server has unexpectedly disconnected

Most likely you are hitting a bug in the NTLM authentication code which seems to not work on some versions of Exchange.

You can disable NTLM auth by doing:

client.AuthenticationMechanisms.Remove ("NTLM");

Call that just before calling the Authenticate method and you will likely be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM