【发布时间】:2017-01-18 04:49:41
【问题描述】:
我是 Web 编程新手,我正在使用 ASP.NET Core 制作网站。我正在尝试创建一个标准的“联系我”页面,用户可以在其中输入姓名、电子邮件、主题和消息。 ASP.NET Core 还没有System.Net.Mail,所以我不能用。
我看到 MailKit 可用于发送电子邮件,但无法弄清楚如何将其用于联系页面。我知道使用此代码
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.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 ("joey", "password");
client.Send (message);
client.Disconnect (true);
我可以使用我的 SMTP 服务器发送电子邮件,但显然我希望使用该站点的用户能够向我发送电子邮件。有没有办法为此使用 MailKit,还是我需要找到另一个解决方案?谢谢。
编辑: 这是我成功发送电子邮件的代码,但它总是说它是从我发送给我的。
public IActionResult SendEmail(Contact contact)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress(contact.Name, contact.Email));
emailMessage.To.Add(new MailboxAddress("myname", "myemail"));
emailMessage.Subject = contact.Subject;
emailMessage.Body = new TextPart("plain") { Text = contact.Message };
using (var client = new SmtpClient())
{
client.Connect("smtp-mail.outlook.com", 587);
client.Authenticate("myemail", "myemailpassword");
client.Send(emailMessage);
client.Disconnect(true);
}
return RedirectToAction("Index", "Home");
}
【问题讨论】:
标签: c# asp.net-mvc email asp.net-core-1.0 mailkit