如何使用asp.net core 1.1从outlook发送电子邮件

时间:2017-06-30 07:09:43

标签: c# mailkit asp.net-core-1.1

任何人都可以帮助我如何从outlook发送来自asp.net核心应用程序的电子邮件。我可以使用MailKit从gmail发送电子邮件,但是它的Outlook失败了

以下是我使用

的代码
            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);

            }

它的抛出错误smptp服务器意外断开

1 个答案:

答案 0 :(得分:0)

很可能您遇到了NTLM身份验证代码中的错误,该错误在某些版本的Exchange上似乎无效。

您可以通过执行以下操作禁用NTLM身份验证:

client.AuthenticationMechanisms.Remove ("NTLM");

在调用Authenticate方法之前调用它,你可能会没事。