Netcore使用MailKit进行邮件发送

Netcore使用MailKit进行邮件发送

public void TestSendMailDemo()
{
var message = new MimeKit.MimeMessage();
message.From.Add(new MimeKit.MailboxAddress(“hotmail”, “china-
psu@hotmail.com“));
message.To.Add(new MimeKit.MailboxAddress(“qq”, “283775652@qq.com“));
message.Subject = “This is a Test Mail”;
var plain = new MimeKit.TextPart(“plain”)
{
Text = @”不好意思,我在测试程序,Sorry!”
};
var html = new MimeKit.TextPart(“html”)
{
Text = @”

Hey geffzhang

不好意思,我在测试程序,Sorry!

\-- Geffzhang
" }; // create an image attachment for the file located at path var path = "D:\\\雄安.jpg"; var fs = File.OpenRead(path); var attachment = new MimeKit.MimePart("image", "jpeg") {

ContentObject = new MimeKit.ContentObject(fs,
MimeKit.ContentEncoding.Default),
ContentDisposition = new
MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
FileName = Path.GetFileName(path)
};
var alternative = new MimeKit.Multipart(“alternative”);
alternative.Add(plain);
alternative.Add(html);
// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new MimeKit.Multipart(“mixed”);
multipart.Add(alternative);
multipart.Add(attachment);
message.Body = multipart;
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(“smtp.live.com”, 587, false);

// 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
var mailFromAccount = “china-psu@hotmail.com“;
var mailPassword = “xxxxxxxxxxxxxxxxxxx”;
client.Authenticate(mailFromAccount, mailPassword);

client.Send(message);
client.Disconnect(true);
}
fs.Dispose();
}


title: Netcore使用MailKit进行邮件发送
date: 2017-04-17 06:19:00
tags:
categories:

public void TestSendMailDemo()
{
var message = new MimeKit.MimeMessage();
message.From.Add(new MimeKit.MailboxAddress(“hotmail”, “china-
psu@hotmail.com“));
message.To.Add(new MimeKit.MailboxAddress(“qq”, “283775652@qq.com“));
message.Subject = “This is a Test Mail”;
var plain = new MimeKit.TextPart(“plain”)
{
Text = @”不好意思,我在测试程序,Sorry!”
};
var html = new MimeKit.TextPart(“html”)
{
Text = @”

Hey geffzhang

不好意思,我在测试程序,Sorry!

\-- Geffzhang
" }; // create an image attachment for the file located at path var path = "D:\\\雄安.jpg"; var fs = File.OpenRead(path); var attachment = new MimeKit.MimePart("image", "jpeg") {

ContentObject = new MimeKit.ContentObject(fs,
MimeKit.ContentEncoding.Default),
ContentDisposition = new
MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
FileName = Path.GetFileName(path)
};
var alternative = new MimeKit.Multipart(“alternative”);
alternative.Add(plain);
alternative.Add(html);
// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new MimeKit.Multipart(“mixed”);
multipart.Add(alternative);
multipart.Add(attachment);
message.Body = multipart;
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(“smtp.live.com”, 587, false);

// 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
var mailFromAccount = “china-psu@hotmail.com“;
var mailPassword = “xxxxxxxxxxxxxxxxxxx”;
client.Authenticate(mailFromAccount, mailPassword);

client.Send(message);
client.Disconnect(true);
}
fs.Dispose();
}


Netcore使用MailKit进行邮件发送
https://www.dearcloud.cn/2017/04/17/20200310-cnblogs-old-posts/20170417-Netcore使用MailKit进行邮件发送/
作者
宋兴柱
发布于
2017年4月17日
许可协议