我可以使用MailKit通过电子邮件发送文件吗? [英] Can I send files via email using MailKit?

查看:511
本文介绍了我可以使用MailKit通过电子邮件发送文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题,是否支持MailKit发送文件?
如果是,该怎么办?

As the title, is MailKit supported to send file?
If yes, how can I do it?

推荐答案

是.在文档以及常见问题解答中对此进行了说明.

Yes. This is explained in the documentation as well as the FAQ.

通过常见问题解答:

要构建带有附件的邮件,您需要做的第一件事是创建multipart/mixed 然后您要先将邮件正文添加到的容器.一旦添加了主体,就可以 然后向其中添加包含您要附加​​的文件内容的MIME部分,请务必进行设置 附件的Content-Disposition标头值.您可能还需要设置filename Content-Disposition标头上的参数以及Content-Type上的name参数 标头.最方便的方法是简单地使用 MimePart.FileName 属性 将为您设置两个参数以及将Content-Disposition标头值设置为attachment 如果尚未将其设置为其他内容.

To construct a message with attachments, the first thing you'll need to do is create a multipart/mixed container which you'll then want to add the message body to first. Once you've added the body, you can then add MIME parts to it that contain the content of the files you'd like to attach, being sure to set the Content-Disposition header value to attachment. You'll probably also want to set the filename parameter on the Content-Disposition header as well as the name parameter on the Content-Type header. The most convenient way to do this is to simply use the MimePart.FileName property which will set both parameters for you as well as setting the Content-Disposition header value to attachment if it has not already been set to something else.

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") {
    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 类.

A simpler way to construct messages with attachments is to take advantage of the BodyBuilder class.

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 and 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 ();

有关更多信息,请参见创建消息.

For more information, see Creating Messages.

这篇关于我可以使用MailKit通过电子邮件发送文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆