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

如何将文件附件添加到从Razor页面发送的电子邮件中(使用ASP.NET Core和MailKit),asp.net,asp.net-mvc,razor,asp.net-core,mailkit,Asp.net,Asp.net Mvc,Razor,Asp.net Core,Mailkit,以下是从ASP.NET Core中的Razor页面发送电子邮件的方法。我需要使用MailKit,因为System.Net.Mail在ASP.Net Core中不可用 尽管做了很多研究,我还是没能找到一种方法将图片包含到电子邮件中。请注意,它不一定是一个附件-嵌入图像将工作 public ActionResult Contribute([Bind("SubmitterScope, SubmitterLocation, SubmitterItem, SubmitterCategory, Su

以下是从ASP.NET Core中的Razor页面发送电子邮件的方法。我需要使用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 repo上的常见问题,似乎涵盖了整个过程。


正如@SimonFox指出的,这个问题可能重复。你必须做的一件事是从纯文本切换到HTML,因为没有办法像你试图做的那样将图像“嵌入”到纯文本消息正文中,这是行不通的。我的问题是,我试图让用户上传附件。似乎由于几乎所有现代浏览器中的安全限制,了解本地文件路径是不可能的。这就是我正在努力解决的问题。@C.H.那么这个问题根本不是关于邮箱的?它是关于用asp.net-core mvc上传一个图像?它是关于用asp.net-core mvc在电子邮件中发送一个图像,是的。我试过这个。它不适用于用户从本地文件路径上载文件的Web应用,因为本地文件路径未知。此代码在服务器上执行,而不是在浏览器中执行。要使用来自用户计算机的文件,您必须先将其上载到服务器。看到这个,明白了-将在链接的帖子中使用代码。谢谢。
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;