邮件发送已经说烂了的功能

我相信每一个系统,都会有邮件发送的场景,而且这种使用场景有很多,比如登入一个系统,忘记密码,那就要通过忘记密码功能,检查你注册邮件,然后发一封重设密码的邮件给你(当然现在可能通过手机验证码处理的方案多)。

到今天为止我一直以为,.NET发送邮件组件有很多,我之前了解的有.Net 自带的System.Net.Mail,JMail,MimeKit&MailKit。

今天我用度娘搜索了一下“发送邮件 组件”,搜索结果大概汇总一下,.Net 自带的,CDO 组件,JMail 组件 就这三个。没有搜索到 MimeKit&MailKit,

但是单独搜“MailKit”能够搜索到,而且是大牛 张善友 写的,那我暂时先说一下 MimeKit&MailKit吧。

友情推荐一下 张善友 http://www.cnblogs.com/shanyou/p/4034298.html

MimeKit&MailKit 外国开源邮件发送组件,其实是由两个组件组成

MimeKit 负责处理寄件者,消息,附件 等等内容相关的。

MimeKit 官网:http://www.mimekit.net/

MimeKit GitHub https://github.com/jstedfast/MimeKit

开发指南:http://www.mimekit.net/docs/html/Introduction.htm

MailKit 负责发送相关的。

MailKit GitHub:https://github.com/jstedfast/MailKit

贴几段代码让大家感受一下

MimeKit 部分

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "[email protected]"));
message.To.Add (new MailboxAddress ("Alice", "[email protected]"));
message.Subject = "How you doin?";

message.Body = new TextPart ("plain") {
    Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
};
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "[email protected]"));
message.To.Add (new MailboxAddress ("Alice", "[email protected]"));
message.Subject = "How you doin?";

// create our message text, just like before (except don‘t set it as the message.Body)
var body = new TextPart ("plain") {
    Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
};

// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
    ContentObject = new ContentObject (File.OpenRead (path), ContentEncoding.Default),
    ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
    ContentTransferEncoding = ContentEncoding.Base64,
    FileName = Path.GetFileName (path)
};

// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);

// now set the multipart/mixed as the message body
message.Body = multipart;
   var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("Joey", "[email protected]"));
            message.To.Add (new MailboxAddress ("Alice", "[email protected]"));
            message.Subject = "How you doin?";

            var builder = new BodyBuilder ();

            // Set the plain-text version of the message text
            builder.TextBody = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
";

            // In order to reference sexy-pose.jpg from the html text, we‘ll need to add it
            // to builder.LinkedResources and then use its Content-Id value in the img src.
            var image = builder.LinkedResources.Add (@"C:\Users\Joey\Documents\Selfies\sexy-pose.jpg");
            image.ContentId = MimeUtils.GenerateMessageId ();

            // Set the html version of the message text
            builder.HtmlBody = string.Format (@"<p>Hey Alice,<br>
<p>What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.<br>
<p>Will you be my +1?<br>
<p>-- Joey<br>
<center><img src=""cid:{0}""></center>", image.ContentId);

            // We may also want to attach a calendar event for Monica‘s party...
            builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");

            // Now we just need to set the message body and we‘re done
            message.Body = builder.ToMessageBody ();

怎么样?是不是感觉老外写得东西,比较好看,易懂啊。

MailKit 部分

using System;

using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

namespace TestClient {
    class Program
    {
        public static void Main (string[] args)
        {
            var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("Joey Tribbiani", "[email protected]"));
            message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "[email protected]"));
            message.Subject = "How you doin‘?";

            message.Body = new TextPart ("plain") {
                Text = @"Hey Chandler,

I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey"
            };

            using (var client = new SmtpClient ()) {
                client.Connect ("smtp.friends.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 ("joey", "password");

                client.Send (message);
                client.Disconnect (true);
            }
        }
    }
}

这个是不是感觉跟.Net 发送邮件部分差不多啊!

下面是我封装的.Net 发送邮件部分代码,做个对比

    public class Mail
    {

        private static SystemMailHostConfig _config;
        public static Mail SystemMail
        {
            get { return new Mail(); }
        }

        public Mail()
        {
            _config = new SystemMailHostConfig
            {
                FromName = MailConfig.SystemMailFromName,
                From = MailConfig.SystemMailFrom,
                MailHostConfig = new MailHostConfig
                {
                    HostName = MailConfig.SmtpHostName,
                    Account = MailConfig.MailAccount,
                    Password = MailConfig.MailPassword
                }
            };
        }

        public Mail(SystemMailHostConfig config)
        {
            _config = config;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">默认为null。 System.Net.Mail.MailMessage </param>
        public void Send(MailContext context, MailMessage message = null)
        {
            //ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
            var sender = new SmtpClient();
            message = message ?? new MailMessage();
            if (context.Receivers.IsNotNullOrEmpty())
            {
                message.To.AddRange(context.Receivers.ToArray());
            }
            if (context.Ccs.IsNotNullOrEmpty())
            {
                message.CC.AddRange(context.Ccs.ToArray());
            }
            if (context.Bccs.IsNotNullOrEmpty())
            {
                message.Bcc.AddRange(context.Bccs.ToArray());
            }
            message.Subject = context.Subject;
            message.Body = context.Body;

            if (context.Attachments.IsNotNullOrEmpty())
            {
                message.Attachments.AddRange(context.Attachments.ToArray());
            }
            message.IsBodyHtml = true;
            try
            {
                message.From = new MailAddress(_config.From, _config.FromName);
                sender.Port = 2620;
                sender.Host = _config.MailHostConfig.HostName;
                sender.UseDefaultCredentials = false;
                sender.Credentials = new NetworkCredential(_config.MailHostConfig.Account, _config.MailHostConfig.Password);
                sender.DeliveryMethod = SmtpDeliveryMethod.Network;
                sender.EnableSsl = true;
                sender.Send(message);
            }
            catch
            {
                _config = new SystemMailHostConfig
                {
                    FromName = MailConfig.StandbySystemMailFromName,
                    From = MailConfig.StandbySystemMailFrom,
                    MailHostConfig = new MailHostConfig
                    {
                        HostName = MailConfig.StandbySmtpHostName,
                        Account = MailConfig.StandbyMailAccount,
                        Password = MailConfig.StandbyMailPassword
                    }
                };
                message.From = new MailAddress(_config.From, _config.FromName);
                sender.Port = 587;
                sender.Host = _config.MailHostConfig.HostName;
                sender.UseDefaultCredentials = false;
                sender.Credentials = new NetworkCredential(_config.MailHostConfig.Account, _config.MailHostConfig.Password);
                sender.DeliveryMethod = SmtpDeliveryMethod.Network;
                sender.EnableSsl = true;
                sender.Send(message);
            }
        }
    }

MimeKit 和 MailKit 支持最新的国际化的电子邮件标准,是.NET 中为一个支持完整支持这些标准电子邮件库,且它还支持.NET/Mono的所有平台,包括移动电话、平板等,希望大家在架构多一种方案,多一种选择。

时间: 2024-12-11 01:10:20

邮件发送已经说烂了的功能的相关文章

Java邮件发送与屏幕截屏

前几天七夕情人节孤独寂寞的程序猿闲来没事,花了一两个小时写了个小Demo主要实现Java的Mail发送功能和桌面截屏功能. 首先让我们先看看Java sendMail邮件发送和桌面屏幕截屏功能是怎么实现的基础知识. 一.Java  SendMail邮件发送 首先让我们来看看邮件发送的原理图: JavaMail 是一套sun 提供开发邮件收发程序API,JavaMail编写程序就是邮件客户端程序(和outlook.foxmail功能类似) * JavaMail开发需要类库 javamail API

Spring+Freemarker模板邮件发送

Spring+Freemarker模板邮件发送 在eclipse中新建一个Java Project, 名称为spring-freemarker-mail 2. 在项目下新建一个lib目录,导入下面需要的jar包 commons-logging-1.2.jar freemarker.jar mail.jar servlet-api.jar spring-beans-3.2.2.jar spring-context-3.2.2.jar spring-context-support-3.2.2.jar

redmine邮件发送功能配置详解

redmine的邮件发送功能还是很有用的. 像项目有更新啦,任务分配啦,都能邮件发送的相关责任人. 我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了. 查了网上的资料,都是讲修改下配置文件就可以了,他们没错,只是没有讲全. 下面是我整理的一个redmine邮件发送功能设置的一个完整流程. 1. sendmail安装与检查 linux机器上安装的redmine要能发送邮件,先得是本机的sendmail功能是正常的. 查看sendmail进程是否已正常启动: $ ps au

用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇

在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还是很耐心的. 最终结论,是由于MailKit发送了两次EHLO命令,查看了MailKit源码后竟然发现,里面写了硬编码: if (host != "smtp.strato.de" && host != "smtp.sina.com") Ehlo (can

邮件发送功能开发

作为一名.Net开发,"邮件发送"功能的开发和使用是必须要掌握的,因为这个功能作为"消息推送"的一种手段经常出现在各种.Net系统中,所以本文将对.Net平台下的"邮件发送",做一个细致的分析! 一.who需要邮件功能 1.服务提供方:需提供邮件收发客户端或Web服务.如:QQ邮箱.GMail邮箱.126.163等知名邮件服务提供商.注:如果你使用的第三方不知名邮件服务商提供的邮件收发服务,通过其发出的邮件,可能会被其他知名邮件服务提供商的STM

ThinkPHP中邮件发送功能

初次使用thinkphp框架,开发一个邮件发送功能,由于对框架不熟悉折腾了几个小时终于成功了,以下是代码记录. 此函数只能在ThinkPHP中使用且需要phpmailer扩展的支持:phpmail的下载地址:https://code.google.com/a/apache-extras.org/p/phpmailer 将phpmailer解压后放置扩展放置到第三方类库扩展目录下: ThinkPHP/Extend/Vendor/文件夹下即可,并使用vendor方法来导入.更详细介绍参考:http:

Swift - 邮件发送功能的实现

使用MessageUI.framework框架除了可以发送短信,还能发送Email,步骤如下: (1)首先判断设备是否有发送邮件功能 (2)如果设备允许发送邮件,创建一个MFMailComposeViewController视图控制器,并对其设置邮件标题,正文内容,收件人,附件等. (3)邮件发送后,会执行回调代理方法,可以获取发送结果(成功,失败或取消) 效果图如下: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

C#实现简单的邮件发送功能

唉,最近要做一个项目,里面需要实现邮件发送功能.在网络上也找一些看,自己也随便写下.也当做是给自己复习下吧,如有不对之处还请大家指出.谢谢! 首先我是创建一个实体对象Model (EmailParameterSet) : 1 public class EmailParameterSet 2 { 3 /// <summary> 4 /// 收件人的邮件地址 5 /// </summary> 6 public string ConsigneeAddress { get; set; }

xampp 和thinkphp 建一个本地站并实现邮件发送功能

一.xampp的安装使用 1.首先看下什么是xampp,想要建站的小伙伴肯定都知道,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建 XAMPP 软件站集成软件包. 它可以在Windows.Linux.Solaris.Mac OS X 等多种操作系统下安装使用 2.需要什么版本到官网上下载:xampp下载 3.安装很简单,傻瓜式安装,感觉mysql选项可以去掉,自己安装一个,具体安装看这个:mysql安装,另外路径建议只改动盘符,保留后缀路径 4.安装好的目录如下 5.