发送带有附件的电子邮件

发送带有附件的电子邮件

本文介绍了如何在asp.net核心Web API中使用MimeMessage发送带有附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只尝试使用简单文本,但是我想发送带有附件的电子邮件.

I've tried with only simple text but i want to sent email with attachment.

    var emailMessage = new MimeMessage();
    emailMessage.From.Add(new MailboxAddress("Test","[email protected]"));
    emailMessage.To.Add(new MailboxAddress("Demo", "[email protected]"));
    emailMessage.Subject = "Hello";
    emailMessage.Body = new TextPart("html") { Text = "Hi............" };

//我想在此处附上正文.

    //Send Email.
    using (var client = new SmtpClient())
    {
            await client.ConnectAsync("smtp.gmail.com", 587, false);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            await client.AuthenticateAsync("uid", "pass");
            await client.SendAsync(emailMessage);
            await client.DisconnectAsync(true);
    }

推荐答案

    var multipart = new Multipart("mixed");
    multipart.Add(new TextPart("html") { Text = "your body message"});

    // 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)
    };

    multipart.Add(attachment);
    emailMessage.Body = multipart;

有关更多详细信息,请访问此处

For more detail, Please visit here

这篇关于如何在asp.net核心Web API中使用MimeMessage发送带有附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:04