[英]Send MailKit email with an attachment from memoryStream
How to send an email with an attachment from memoryStream using MailKit?如何使用 MailKit 从 memoryStream 发送带有附件的 email? Currently, I'm sending using regular SMTP and attaching the file using the below code, but couldn't find any proper example to send it using MailKit package.
目前,我正在使用常规 SMTP 发送并使用以下代码附加文件,但找不到任何合适的示例来使用 MailKit package 发送它。 I have gone through this two docs, but couldn't find a proper solution.
我已经浏览了这两个文档,但找不到合适的解决方案。 http://www.mimekit.net/docs/html/M_MimeKit_AttachmentCollection_Add_6.htm
http://www.mimekit.net/docs/html/M_MimeKit_AttachmentCollection_Add_6.htm
using System.Net.Mail;
MemoryStream memoryStream = new MemoryStream(bytes);
message.Attachments.Add(new Attachment(memoryStream, "Receipt.pdf", MediaTypeNames.Application.Pdf));
This is my MailKit email code:这是我的 MailKit email 代码:
#region MailKit
string fromEmail = GlobalVariable.FromEmail;
string fromEmailPwd = "";//add sender password
var email = new MimeKit.MimeMessage();
email.From.Add(new MimeKit.MailboxAddress("Sender", fromEmail));
email.To.Add(new MimeKit.MailboxAddress("receiver", "receiver@gmail.com"));
var emailBody = new MimeKit.BodyBuilder
{
HtmlBody = htmlString
};
email.Subject = "test Booking";
email.Body = emailBody.ToMessageBody();
//bytes is parameter.
//MemoryStream memoryStream = new MemoryStream(bytes);
//message.Attachments.Add(new Attachment(memoryStream, "Receipt.pdf", MediaTypeNames.Application.Pdf));
using (var smtp = new MailKit.Net.Smtp.SmtpClient())
{
smtp.Connect("smtp.gmail.com", 465, true);
smtp.Authenticate(fromEmail, fromEmailPwd);
smtp.Send(email);
smtp.Disconnect(true);
}
#endregion
This is how it's done: you need to create a TextPart
for the string content and a MimePart
for the attachment and add both to a Multipart
which is the Body
of the MimeMessage
这就是它的完成方式:您需要为字符串内容创建一个
TextPart
,为附件创建一个Body
,并将两者都添加到作为MimeMessage
MimePart
的Multipart
I assumed that you want to send an HTML string textContent
and a PDF file with name filename
which is already read using any stream named stream
. I assumed that you want to send an HTML string
textContent
and a PDF file with name filename
which is already read using any stream named stream
.
var multipart = new Multipart("mixed");
var textPart = new TextPart(TextFormat.Html)
{
Text = textContent,
ContentTransferEncoding = ContentEncoding.Base64,
};
multipart.Add(textPart);
stream.Position = 0; // you MUST reset stream position
var attachmentPart = new MimePart(MediaTypeNames.Application.Pdf)
{
Content = new MimeContent(stream),
ContentId = filename,
ContentTransferEncoding = ContentEncoding.Base64,
FileName = filename
};
multipart.Add(attachmentPart);
mimeMessage.Body = multipart;
Note that as for contentType
I used MediaTypeNames.Application.Pdf
from DLL System.Net.Mail
and namespace System.Net.Mime
, which equals the string "application/pdf"
.请注意,对于
contentType
,我使用了来自 DLL System.Net.Mail
和命名空间System.Net.Mime
的MediaTypeNames.Application.Pdf
,它等于字符串"application/pdf"
。 You may instead use any other library that you like, or write your own.您可以改用您喜欢的任何其他库,或者编写自己的库。
If you'd like to stick with the MimeKit's BodyBuilder to build your message body, you can do something like this:如果您想坚持使用 MimeKit 的 BodyBuilder 来构建您的消息正文,您可以执行以下操作:
var emailBody = new MimeKit.BodyBuilder
{
HtmlBody = htmlString
};
emailBody.Attachments.Add ("Receipt.pdf", bytes);
// If you find that MimeKit does not properly auto-detect the mime-type based on the
// filename, you can specify a mime-type like this:
//emailBody.Attachments.Add ("Receipt.pdf", bytes, ContentType.Parse (MediaTypeNames.Application.Pdf));
message.Body = emailBody.ToMessageBody ();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.