将数据表单转换为asp.net核心中的电子邮件地址

时间:2017-02-07 11:21:37

标签: asp.net-mvc asp.net-core

在asp.net核心应用中,将表单数据发送到电子邮件地址的最简单方法是什么?我试图从我的Outlook帐户发送一封电子邮件给一个机智的Mailkit,但我不断收到错误AuthenticationException:AuthenticationInvalidCredentials,即使我的凭据是核心。

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "joey@outlook.com"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "Chandler@gmail.com"));
message.Subject = "How you doin'?";

message.Body = new TextPart("plain")
{
    Text = @"Hey Chandler"
};

using (var client = new SmtpClient())
{
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;

    client.Connect("smtp.outlook.com", 587, false);

    // Note: since we don't have an OAuth2 token, disable
    // the XOAUTH2 authentication mechanism.
    client.AuthenticationMechanisms.Remove("XOAUTH2");

    // Note: only needed if the SMTP server requires authentication
    client.Authenticate("username", "*************");

    client.Send(message);
    client.Disconnect(true);
}

0 个答案:

没有答案