欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MailKit/MimeKit 发送邮件

程序员文章站 2022-05-03 14:49:34
MimeKit / MailKit 支持最新的国际化的电子邮件标准,是.NET 中为一个支持完整支持这些标准电子邮件库,最近正式发布了1.0版本。如果你想做所有与的电子邮件相关的事情,看看 MimeKit 和 MailKit。我保证你不会失望,它支持.NET/Mono的所有平台,包括移动电话、平板等 ......

mimekit / mailkit 支持最新的国际化的电子邮件标准,是.net 中为一个支持完整支持这些标准电子邮件库,最近正式发布了1.0版本。如果你想做所有与的电子邮件相关的事情,看看 mimekit 和 mailkit。我保证你不会失望,它支持.net/mono的所有平台,包括移动电话、平板等.

废话不多说,直接上代码:

using mimekit;
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using mailkit.net.smtp;
using system.io;
 
namespace netsmtpclient
{
    class program
    {
        const string mailfrom = "xxxx@hotmail.com";
        const string mailto = "xxxx@qq.com";
        const string mailfromaccount = "xxxx@hotmail.com";
        const string mailpassword = "xxxx";
        const string path = @"e:\github\testmailclient\netsmtpclient\.netfoundation.png";
        static void main(string[] args)
        {
            testsmtpclient();
 
            testmailkit();
 
        }
 
        private static void testmailkit()
        {
            var message = new mimemessage();
            message.from.add(new mailboxaddress("geffzhang", mailfrom));
            message.to.add(new mailboxaddress("geffzhang", mailto));
            message.subject = string.format("c#自动发送邮件测试 from geffzhang to {0}", mailto);
 
            var plain = new textpart("plain")
            {
                text = @"不好意思,我在测试程序,刚才把qq号写错了,sorry!"
            };
            var html = new textpart("html")
            {
                text = @"<p>hey geffzhang<br>
<p>不好意思,我在测试程序,刚才把qq号写错了,sorry!<br>
<p>-- geffzhang<br>"
            };
            // create an image attachment for the file located at path
            var attachment = new mimepart("image", "png")
            {
                contentobject = new contentobject(file.openread(path), contentencoding.default),
                contentdisposition = new contentdisposition(contentdisposition.attachment),
                contenttransferencoding = contentencoding.base64,
                filename = path.getfilename(path)
            };
           attachment.contenttype.parameters.add("gb18030", "name", filename);
           attachment.contentdisposition.parameters.add("gb18030", "filename", filename)var alternative = new 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 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
client.authenticate(mailfromaccount, mailpassword); client.send(message); client.disconnect(true);
}
}
}
}

上面代码是smtp发送代码,这个库还支持pop3, imap 等。