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

MailKit/MimeKit 发送邮件的更多相关文章

  1. .netcore使用MimeKit发送邮件

    以163邮箱为例,借助MimeKit nuget安装:MimeKit类库,源码地址:https://github.com/jstedfast/MimeKit 发送方法如下: #region 邮件发送 ...

  2. MailKit和MimeKit 收发邮件

    新建项目,引用MailKit和MimeKit NuGet包 using CommonTool.MailKit; using System; using System.Collections.Gener ...

  3. AspNetCore 目前不支持SMTP协议(基于开源组件开发邮件发送,它们分别是MailKit 和 FluentEmail )

    net所有的功能都要重新来一遍,集成众多类库,core任重道远,且发展且努力!! 我们都知道,很多的邮件发送都是基于这个SMTP协议,但现在的.net core对这方面还不太支持,所以我们选择这两个组 ...

  4. 如何在 ASP.NET Core 中发送邮件

    前言 我们知道目前 .NET Core 还不支持 SMTP 协议,当我么在使用到发送邮件功能的时候,需要借助于一些第三方组件来达到目的,今天给大家介绍两款开源的邮件发送组件,它们分别是 MailKit ...

  5. Asp.Net Core MailKit 完美附件(中文名、长文件名)

    最近在使用MailKit组件发送邮件,看了一些博客其实还是蛮简单的,但是发送附件的时候却产生了不小的问题,附件的中文名字是乱码的,或者附件的名字过长就会无效,附件的名字在QQ邮箱中会变成类似 tcmi ...

  6. .net core 2.x - 发送邮件

    前言 我们知道目前 .NET Core 还不支持 SMTP 协议,当我么在使用到发送邮件功能的时候,需要借助于一些第三方组件来达到目的,今天给大家介绍两款开源的邮件发送组件,它们分别是 MailKit ...

  7. DotNet 资源大全中文版(Awesome最新版)

    Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...

  8. 《.NET开发资源大全》

    目录 API 应用框架(Application Frameworks) 应用模板(Application Templates) 人工智能(Artificial Intelligence) 程序集处理( ...

  9. DotNet 资源大全中文版

    https://blog.csdn.net/fhzh520/article/details/52637545 目录 算法与数据结构(Algorithms and Data structures) 应用 ...

随机推荐

  1. ASIO库使用注意事项

    1. 使用 io_service::work 实现 io_service 无任务时不退出 正常情况下向io_service抛任务,它执行完成后就会自动退出,而要实现那种chromium那种的循环队列, ...

  2. Appium基础:Desired Capabilities详讲

    Desired Capabilities在启动session的时候是必须提供的,先看如下代码: Desired Capabilities本质上是key value的对象,他告诉appium serve ...

  3. python函数中的不定长参数

    python自定义函数中有两中不定长参数,第一种是*name,第二种是**name.加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数.加了两个星号 ** 的参数会以字典的 ...

  4. thread_asyncio

    thread_asyncio # 使用多线程:在携程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor i ...

  5. 【Spring IoC】IoC介绍(一)

    IoC(Inversion of Control)的职责:原先由程序员主动通过new实例化对象这个事情,现在交由Spring负责,即由IoC容器负责. Spring 容器是 Spring 框架的核心. ...

  6. LG5357 「模板」AC自动机(二次加强版) AC自动机+fail树

    问题描述 LG5357 题解 不是fail树的AC自动机复杂度是假的. 把AC自动机搞出来,建立Trie树,树上爆搜一遍就好了. \(\mathrm{Code}\) #include<bits/ ...

  7. Spring Boot 引入外部yml配置文件

    当需要在springboot中引用其他的yml文件时,需要在application.yml里配置 spring:     profiles:         include: email,xmyb   ...

  8. vue动画效果出现重叠,并且出现滚动条

    背景 使用 vue 结合 animated css 第三方动画样式,简单地给页面组件加上切换时的 fade 淡入/淡出动画效果 当调试效果时发现,展示效果出现了问题,并且出现滚动条 原因 退场动画还没 ...

  9. 打开navicat 报错????

    好心好意打开数据库图形化管理工具居然报错了???? 那怎么办呢????? google一下吧...... 下载两个文件 就是这两个 放入你的图形化页面的目录中 就可以正常打开了 帅不帅??? 那怎么下 ...

  10. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...