gpt4 book ai didi

c# - 我可以使用 MailKit 通过电子邮件发送文件吗?

转载 作者:IT王子 更新时间:2023-10-29 04:16:14 27 4
gpt4 key购买 nike

如题,是否支持MailKit发送文件?
如果是,我该怎么做?

最佳答案

是的。这在文档和 FAQ 中有解释。 .

来自常见问题解答:

如何创建带附件的消息?

要构造带有附件的消息,您需要做的第一件事是创建一个 multipart/mixed 容器,然后您要先将消息正文添加到该容器中。添加正文后,您可以向其中添加包含要附加的文件内容的 MIME 部分,确保将 Content-Disposition header 值设置为附件.您可能还想在 Content-Disposition header 上设置 filename 参数,在 Content 上设置 name 参数-Type header 。最方便的方法是简单地使用 MimePart.FileName属性(property)其中将为您设置这两个参数,并将 Content-Disposition header 值设置为 attachment(如果尚未设置为其他值)。

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. 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") {
Content = new MimeContent (File.OpenRead (path)),
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;

构造带有附件的消息的更简单方法是利用 BodyBuilder类。

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?";

var builder = new BodyBuilder ();

// Set the plain-text version of the message text
builder.TextBody = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday. I was hoping you could make it.

Will you be my +1?

-- Joey
";

// We may also want to attach a calendar event for Monica's party...
builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");

// Now we just need to set the message body and we're done
message.Body = builder.ToMessageBody ();

有关详细信息,请参阅 Creating Messages .

关于c# - 我可以使用 MailKit 通过电子邮件发送文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com