zoukankan      html  css  js  c++  java
  • MailKit/MimeKit 发送邮件

    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:GitHubTestMailClientNetSmtpClient.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 等。

  • 相关阅读:
    SharpReader的效率:支持meme聚合
    RSS阅读器:从订阅到发现之旅?
    关于word使用WildCards进行查找和替换
    Cannot resolve plugin org.apache.maven.plugins:mavencleanplugin:2.5
    MyBatis
    python matplotlib中axes与axis subplot的区别是什么?
    MyBatis中settings属性配置详解
    IDEA中 Project 和 Module 的区别
    Pycharm 运行程序后如何 如何查看变量的值(不通过debug的方式)
    查看oracle是否正常、表空间 (AIX)
  • 原文地址:https://www.cnblogs.com/xtxk110/p/11660594.html
Copyright © 2011-2022 走看看