[英]Can MailKit be used for “Contact Me” form?
我是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");
}
是的,MailKit可以用於此目的。
您看到的問題是因為outlook.com用您的電子郵件地址替換了From標頭(當您通過SMTP登錄時,這是Outlook.com的“功能”)。
通過將ReplyTo設置為用戶的地址,您也許可以解決此問題(以便您可以回復他們)。
或者,您可以嘗試將“發件人”設置為用戶的地址,然后將“發件人”設置為您的地址(不確定是否可以使用)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.