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 = @"<p>Hey geffzhang<br>
<p>不好意思,我在测试程序,Sorry!<br>
<p>-- Geffzhang<br>"
};
// 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();
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM