如何将文件附件添加到从 Razor 页面发送的电子邮件(使用 ASP.NET Core 和 MailKit)



以下是从 Core ASP.NET Puror 页面发送电子邮件的方法。我需要使用 MailKit,因为 System.Net.Mail 在 ASP.NET Core 中不可用。

尽管进行了大量研究,但我仍然无法找到将图像包含在电子邮件中的方法。请注意,它不必是附件 - 嵌入图像将起作用。

public ActionResult Contribute([Bind("SubmitterScope, SubmitterLocation, SubmitterItem, SubmitterCategory, SubmitterEmail, SubmitterAcceptsTerms, SubmitterPicture")]
EmailFormModel model)
{
if (ModelState.IsValid)
{
try
{
var emailName= _appSettings.EmailName;
var emailAddress = _appSettings.EmailAddress;
var emailPassword = _appSettings.EmailPassword;
var message = new MimeMessage();
message.From.Add(new MailboxAddress(emailName, emailAddress));
message.To.Add(new MailboxAddress(emailName, emailAddress));
message.Subject = "Record Submission From: " + model.SubmitterEmail.ToString();
message.Body = new TextPart("plain")
{
Text = "Scope: " + model.SubmitterScope.ToString() + "n" +
"Zip Code: " + model.SubmitterLocation.ToString() + "n" +
"Item Description: " + model.SubmitterItem.ToString() + "n" +
"Category: " + model.SubmitterCategory + "n" +
"Submitted By: " + model.SubmitterEmail + "n" +
// This is the file that should be attached.
//"Picture: " + model.SubmitterPicture + "n" +
"Terms Accepted: " + model.SubmitterAcceptsTerms + "n"
};
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587);
// 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(emailAddress, emailPassword);
client.Send(message);
client.Disconnect(true);
return RedirectToAction("Success");
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ": " + ex.StackTrace);
return RedirectToAction("Failure");
}
}
else
{
return View();
}
}

这是来自Mailkit github存储库上的FAQ,似乎涵盖了整个过程。 https://github.com/jstedfast/MailKit/blob/master/FAQ.md#CreateAttachments

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";
// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
Text = @"Hey Alice,
What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.
Will you be my +1?
-- Joey
"
};
// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
ContentObject = new ContentObject (File.OpenRead (path), ContentEncoding.Default),
ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName (path)
};
// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);
// now set the multipart/mixed as the message body
message.Body = multipart;

相关内容

最新更新