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", , 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 等。

最新文章

  1. React的双向绑定
  2. Comparison method violates its general contract 关于jdk自带算法问题
  3. jquery 原理
  4. const变量赋值报错分析
  5. hmmer 使用(转载)
  6. UVALive - 6952 Cent Savings dp
  7. Linux服务器集群系统(一)--转
  8. 学习红帽企业版RHEL 6.4的两问
  9. boost库在windows下的编译和使用
  10. 转载:MyEclipse启动Tomcat缓慢的原因及解决办法
  11. C++中顶层const和底层const
  12. 重启OpenStack服务步骤
  13. 基于Struts自定义MVC-1
  14. Oracle 11g RAC 修改各类IP地址
  15. Hibernate--使用xml配置映射关系
  16. ajax 图片上传
  17. ubuntu 16.04 更改默认Python版本
  18. centos7.4/rehat7.0系统安装
  19. libdl.so 动态库加载、查找
  20. php脚本#!/usr/bin/env php写法的好处

热门文章

  1. 3-2-Pandas 索引
  2. Delphi-基础(例程、例程返回值)
  3. LearnOpenGL.PBR.理论
  4. 06点击事件 tabBar配置 拨打电话
  5. 201871010109-胡欢欢《面向对象程序设计(java)》第十二周学习总结
  6. 201871010111-刘佳华《面向对象程序设计(java)》第二周学习总结
  7. C#基础表达式语句详解(上)
  8. 03-numpy-笔记-expand_dims
  9. 微信小程序 - 组件 | 自定义组件 | 组件事件传递页面
  10. LG4171/BZOJ1823 「JSOI2010」满汉全席 2-SAT