深入Lumisoft.NET组件与.NET API实现邮件发送功能的对比分析

我在另一篇文章《深入Lumisoft.NET实现邮件发送功能的方法详解》有大致对这个Lumisoft.NET组件的使用进行了介绍,当然Lumisoft.NET组件除了提供邮件发送功能外,还提供了邮件接收等功能的处理(包括基于POP3协议和IMAP协议),而.NET则除了提供SMTP协议功能外,则没有提供POP3协议处理的相关类库,因此收取邮件这需要自己进行封装(需要也可以参考codeproject.com上的相关文章)。

1、.NET的邮件发送功能实现
.NET本身封装了一个SmtpClient类以及相关的邮件对象类,这样利用这些类库,也可以方便实现邮件的发送功能的了。

如添加发送人地址,抄送地址,以及暗送地址(多个地址用逗号分开)代码如下。


代码如下:

string toEmails = mailInfo.ToEmail;

string bcc = "";
            mailInfo.RecipientBCC.ForEach(obj => bcc += string.Format("{0},", obj));
            bcc = bcc.Trim(',');

string cc = "";
            mailInfo.RecipientCC.ForEach(obj => cc += string.Format("{0},", obj));
            cc = cc.Trim(',');

MailMessage mail = new MailMessage(settingInfo.MailFrom, toEmails);
            if (!string.IsNullOrEmpty(bcc))
            {
                mail.Bcc.Add(bcc);
            }
            if (!string.IsNullOrEmpty(cc))
            {
                mail.CC.Add(cc);
            }

.NET的附件和嵌入式资源由对象Attachment和LinkedResource进行管理,他们的利用代码如下所示:


代码如下:

//附件
            foreach (string fileName in mailInfo.Attachments)
            {
                mail.Attachments.Add(new Attachment(fileName));
            }

//嵌入资源
            AlternateView view = AlternateView.CreateAlternateViewFromString(mailInfo.Body, Encoding.UTF8, MediaTypeNames.Text.Html);
            foreach (LinkedAttachementInfo link in mailInfo.EmbedObjects)
            {
                LinkedResource resource = new LinkedResource(link.Stream, link.MimeType);
                resource.ContentId = link.ContentId;
                view.LinkedResources.Add(resource);
            }
            mail.AlternateViews.Add(view);

发送邮件的其他部分代码如下所示


代码如下:

mail.IsBodyHtml = mailInfo.IsBodyHtml;
            mail.BodyEncoding = Encoding.UTF8;
            mail.Subject = mailInfo.Subject;
            mail.SubjectEncoding = Encoding.UTF8;

//发送账户设置信息
            SmtpClient client = new SmtpClient();
            client.Host = settingInfo.SmtpServer;
            client.Port = settingInfo.SmptPort;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(settingInfo.SmtpUser, settingInfo.SmtpPass);

bool success = false;
            try
            {
                client.Send(mail);
                success = true;
            }
            catch (Exception ex)
            {
                LogTextHelper.Error(ex);
                //throw;
            }

上面利用.net的SmtpClient发送邮件操作的完整代码如下:


代码如下:

/// <summary>
        /// 发送外部邮件(系统配置,系统邮件)
        /// </summary>
        /// <param name="mailInfo">发送邮件信息</param>
        /// <returns></returns>
        public CommonResult Send(MailInfo mailInfo)
        {
            CommonResult result = new CommonResult();
            try
            {
                AppConfig config = new AppConfig();
                string MailDomain = config.AppConfigGet("MailDomain");
                string MailUsername = config.AppConfigGet("MailUsername");
                string MailPassword = config.AppConfigGet("MailPassword");
                string MailPort = config.AppConfigGet("MailPort");
                string MailFrom = config.AppConfigGet("MailFrom");
                int port = 25;
                int.TryParse(MailPort, out port);

SmtpSettingInfo settingInfo = new SmtpSettingInfo(MailDomain, port,
                    MailUsername, MailPassword, MailFrom);

result.Success = PrivateSendEmail(mailInfo, settingInfo);
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                throw;
            }

return result;
        }

/// <summary>
        /// 通用发送邮件操作
        /// </summary>
        private static bool PrivateSendEmail(MailInfo mailInfo, SmtpSettingInfo settingInfo)
        {         
            string toEmails = mailInfo.ToEmail;

string bcc = "";
            mailInfo.RecipientBCC.ForEach(obj => bcc += string.Format("{0},", obj));
            bcc = bcc.Trim(',');

string cc = "";
            mailInfo.RecipientCC.ForEach(obj => cc += string.Format("{0},", obj));
            cc = cc.Trim(',');

MailMessage mail = new MailMessage(settingInfo.MailFrom, toEmails);
            if (!string.IsNullOrEmpty(bcc))
            {
                mail.Bcc.Add(bcc);
            }
            if (!string.IsNullOrEmpty(cc))
            {
                mail.CC.Add(cc);
            }

//附件
            foreach (string fileName in mailInfo.Attachments)
            {
                mail.Attachments.Add(new Attachment(fileName));
            }

//嵌入资源
            AlternateView view = AlternateView.CreateAlternateViewFromString(mailInfo.Body, Encoding.UTF8, MediaTypeNames.Text.Html);
            foreach (LinkedAttachementInfo link in mailInfo.EmbedObjects)
            {
                LinkedResource resource = new LinkedResource(link.Stream, link.MimeType);
                resource.ContentId = link.ContentId;
                view.LinkedResources.Add(resource);
            }
            mail.AlternateViews.Add(view);
            mail.IsBodyHtml = mailInfo.IsBodyHtml;
            mail.BodyEncoding = Encoding.UTF8;
            mail.Subject = mailInfo.Subject;
            mail.SubjectEncoding = Encoding.UTF8;

//发送账户设置信息
            SmtpClient client = new SmtpClient();
            client.Host = settingInfo.SmtpServer;
            client.Port = settingInfo.SmptPort;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(settingInfo.SmtpUser, settingInfo.SmtpPass);

bool success = false;
            try
            {
                client.Send(mail);
                success = true;
            }
            catch (Exception ex)
            {
                LogTextHelper.Error(ex);
                //throw;
            }

string message = string.Format("发送给【{0}】的邮件“{1}”,{2},时间:{3}",
                mailInfo.ToEmail[0], mailInfo.Subject, success ? "发送成功" : "发送失败", DateTime.Now);
            LogTextHelper.Info(message);

return success;
        }

2、基于Lumisoft.NET组件的邮件发送功能实现

基于Lumisoft.NET组件的邮件发送,也是一种很常用的,因为这个开源组件非常强大,经常可以在一些程序中被使用。

这个发送邮件的功能主要是利用SMTP_Client类来实现的,如下代码所示。注意其中的Authenticate函数已经被舍弃,可以使用Auth方法进行验证。但是函数参数有所不同,根据验证对象,使用不同的验证方式,一般选择AUTH_SASL_Client_Plain对象即可。


代码如下:

public bool Send()
        {
            bool sended = false;
            using (SMTP_Client client = new SMTP_Client())
            {
                client.Connect(smtpServer, smtpPort, smtpUseSsl);
                client.EhloHelo(smtpServer);
                var authhh = new AUTH_SASL_Client_Plain(username, password);
                client.Auth(authhh);
                //client.Authenticate(username, password);
                //string text = client.GreetingText;
                client.MailFrom(from, -1);
                foreach (string address in toList.Keys)
                {
                    client.RcptTo(address);
                }

//采用Mail_Message类型的Stream
                Mail_Message m = Create_PlainText_Html_Attachment_Image(toList, ccList, from, fromDisplay, subject, body, attachments);
                using (MemoryStream stream = new MemoryStream())
                {
                    m.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
                    stream.Position = 0;
                    client.SendMessage(stream);

sended = true;
                }
                if (m != null)
                {
                    m.Dispose();
                }

client.Disconnect();
            }
            return sended;
        }

构造用于SMTP发送的数据,可以使用Mail_Message 对象,也可以使用Mime对象,虽然读都可以实现发送功能,不过Mime对象是舍弃的对象了。

构造Mail_Message对象后,创建用于发送的格式要转换为Stream对象。转换为发送的Stream操作如下所示。


代码如下:

using (MemoryStream stream = new MemoryStream())
{
        m.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
        stream.Position = 0;
        client.SendMessage(stream);

sended = true;
 }

构造Mail_Message格式的邮件操作如下所示。


代码如下:

private Mail_Message Create_PlainText_Html_Attachment_Image(Dictionary<string,string> tomails, Dictionary<string, string> ccmails, string mailFrom, string mailFromDisplay,
            string subject, string body, Dictionary<string, string> attachments, string notifyEmail = "", string plaintTextTips = "")
        {
            Mail_Message msg = new Mail_Message();
            msg.MimeVersion = "1.0";
            msg.MessageID = MIME_Utils.CreateMessageID();
            msg.Date = DateTime.Now;
            msg.Subject = subject;
            msg.From = new Mail_t_MailboxList();
            msg.From.Add(new Mail_t_Mailbox(mailFromDisplay, mailFrom));
            msg.To = new Mail_t_AddressList();
            foreach (string address in tomails.Keys)
            {
                string displayName = tomails[address];
                msg.To.Add(new Mail_t_Mailbox(displayName, address));
            }
            msg.Cc = new Mail_t_AddressList();
            foreach (string address in ccmails.Keys)
            {
                string displayName = ccmails[address];
                msg.Cc.Add(new Mail_t_Mailbox(displayName, address));
            }

//设置回执通知
            if (!string.IsNullOrEmpty(notifyEmail) && ValidateUtil.IsEmail(notifyEmail))
            {
                msg.DispositionNotificationTo.Add(new Mail_t_Mailbox(notifyEmail, notifyEmail));
            }

#region MyRegion

//--- multipart/mixed -----------------------------------
            MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
            contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
            MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
            msg.Body = multipartMixed;

//--- multipart/alternative -----------------------------
            MIME_Entity entity_multipartAlternative = new MIME_Entity();
            MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);
            contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
            MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative);
            entity_multipartAlternative.Body = multipartAlternative;
            multipartMixed.BodyParts.Add(entity_multipartAlternative);

//--- text/plain ----------------------------------------
            MIME_Entity entity_text_plain = new MIME_Entity();
            MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
            entity_text_plain.Body = text_plain;

//普通文本邮件内容,如果对方的收件客户端不支持HTML,这是必需的
            string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
            if (!string.IsNullOrEmpty(plaintTextTips))
            {
                plainTextBody = plaintTextTips;
            }

text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
            multipartAlternative.BodyParts.Add(entity_text_plain);

//--- text/html -----------------------------------------
            string htmlText = body;//"<html>这是一份测试邮件,<img src=\"cid:test.jpg\">来自<font color=red><b>LumiSoft.Net</b></font></html>";
            MIME_Entity entity_text_html = new MIME_Entity();
            MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
            entity_text_html.Body = text_html;
            text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, htmlText);
            multipartAlternative.BodyParts.Add(entity_text_html);

//--- application/octet-stream -------------------------
            WebClient client = new WebClient();
            foreach (string attach in attachments.Keys)
            {
                try
                {
                    byte[] bytes = client.DownloadData(attach);
                    using (MemoryStream stream = new MemoryStream(bytes))
                    {
                        multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(stream, attachments[attach]));
                    }
                }
                catch (Exception ex)
                {
                    LogTextHelper.Error(ex);
                }
            }

#endregion

return msg;
        }

而构造Mime格式的操作如下所示。


代码如下:

private Mime Create_Html_Attachment_Image(string mailTo, string mailFrom, string mailFromDisplay,
            string subject, string body, List<string> attachments, Dictionary<string, string> embedImages, string notifyEmail = "", string plaintTextTips = "",
            string replyEmail = "")
        {
            Mime m = new Mime();
            MimeEntity mainEntity = m.MainEntity;

mainEntity.From = new AddressList();
            mainEntity.From.Add(new MailboxAddress(mailFromDisplay, mailFrom));
            mainEntity.To = new AddressList();
            mainEntity.To.Add(new MailboxAddress(mailTo, mailTo));
            mainEntity.Subject = subject;
            mainEntity.ContentType = MediaType_enum.Multipart_mixed;

//设置回执通知
            if (!string.IsNullOrEmpty(notifyEmail) && ValidateUtil.IsEmail(notifyEmail))
            {
                mainEntity.DSN = notifyEmail;
            }

//设置统一回复地址
            if (!string.IsNullOrEmpty(replyEmail) && ValidateUtil.IsEmail(replyEmail))
            {
                mainEntity.ReplyTo = new AddressList();
                mainEntity.ReplyTo.Add(new MailboxAddress(replyEmail, replyEmail));
            }

MimeEntity textEntity = mainEntity.ChildEntities.Add();
            textEntity.ContentType = MediaType_enum.Text_html;
            textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
            textEntity.DataText = body;

//附件
            foreach (string attach in attachments)
            {
                MimeEntity attachmentEntity = mainEntity.ChildEntities.Add();
                attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;
                attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment;
                attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
                FileInfo file = new FileInfo(attach);
                attachmentEntity.ContentDisposition_FileName = file.Name;
                attachmentEntity.DataFromFile(attach);
            }

//嵌入图片
            foreach (string key in embedImages.Keys)
            {
                MimeEntity attachmentEntity = mainEntity.ChildEntities.Add();
                attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;
                attachmentEntity.ContentDisposition = ContentDisposition_enum.Inline;
                attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
                string imageFile = embedImages[key];
                FileInfo file = new FileInfo(imageFile);
                attachmentEntity.ContentDisposition_FileName = file.Name;

//string displayName = Path.GetFileNameWithoutExtension(fileName);
                attachmentEntity.ContentID = key;//BytesTools.BytesToHex(Encoding.Default.GetBytes(fileName));

attachmentEntity.DataFromFile(imageFile);
            }

return m;
        }

综合以上两者的发送功能,都可以实现邮件的发送操作,如下界面是发送邮件界面。

3、LumiSoft.NET存储eml邮件文件以及发送eml文件操作

除了上面的发送普通邮件,Lumisoft还支持吧邮件序列号存储到文件(.eml邮件文件)里面,然后也可以通过把文件读取到流里面,进行发送,对于某种场合,可以把邮件存储到eml文件是一个很好的操作。

存储EML文件的相关操作如下所示。


代码如下:

private void btnCreateFile_Click(object sender, EventArgs e)
        {
            string attachFile = Path.Combine(Application.StartupPath, "Attachment/Hotel2.png");
            List<string> attachments = new List<string>();
            attachments.Add(attachFile);
            string subject = "测试邮件";
            string body = "<html>这是一份测试邮件,来自<font color=red><b>LumiSoft.Net</b></font></html>";
            string bodyEmbedy = "<html>这是一份测试邮件<img src=\"cid:test.jpg\">,来自<font color=red><b>LumiSoft.Net</b></font></html>";
            Dictionary<string, string> embedList = new Dictionary<string, string>();
            embedList.Add("test.jpg", "C:\\test.jpg");

//存储为Eml文件
            string path = Path.Combine(Application.StartupPath, "Eml");
            DirectoryUtil.AssertDirExist(path);
            string emlFile = string.Format("{0}/{1}.eml", path, DateTime.Now.ToFileTime());

Mime m = Create_Html_Attachment_Image(to, from, from, subject, bodyEmbedy, attachments, embedList);
            m.ToFile(emlFile);

MessageUtil.ShowTips("OK");
        }

发送EML文件操作如下所示。


代码如下:

private void btnSendFile_Click(object sender, EventArgs e)
        {
            using (SMTP_Client client = new SMTP_Client())
            {
                int smtpPort = smtpUseSsl ? WellKnownPorts.SMTP_SSL : WellKnownPorts.SMTP;

client.Connect(smtpServer, smtpPort, smtpUseSsl);
                client.EhloHelo(smtpServer);
                //var authhh = new AUTH_SASL_Client_Plain(username, password);
                //client.Auth(authhh);
                client.Authenticate(username, password);
                //string text = client.GreetingText;
                client.MailFrom(from, -1);
                client.RcptTo(to);

string path = Path.Combine(Application.StartupPath, "Eml");
                string emlFile = Directory.GetFiles(path)[0];
                var msg = Mail_Message.ParseFromFile(emlFile);

MemoryStream stream = new MemoryStream();
                msg.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
                stream.Position = 0;
                client.SendMessage(stream);
                client.Disconnect();
            }
            MessageUtil.ShowTips("OK");
        }

(0)

相关推荐

  • MSScriptControl.ScriptControl组件属性、方法、事件介绍

    MSScriptControl.ScriptControl组件实例对象属性 属性名称 类型 备注 AllowUI BOOL 检测是否允许运行用户的接口元素.如果为False,则诸如消息框之类的界面元素不可见. CodeObject Object 脚本暴露给宿主调用的对象.只读. Modules Modules 宿主提供给脚本的组件库模块.只读.(COM组件通常都是以对象收集的形式向用户提供可以留给用户二次开发的对象集合,每一个收集即一个Modules) Language String 设置或获取

  • asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)

    在Web开发中,有很多可以上传的组件模块,利用HTML的File控件的上传也是一种办法,不过这种方式,需要处理的细节比较多,而且只能支持单文件的操作.在目前Web开发中用的比较多的,可能uploadify(参考http://www.uploadify.com/)也算一个吧,不过这个版本一直在变化,他们的脚本调用也有很大的不同,甚至调用及参数都一直在变化,很早的时候,那个Flash的按钮文字还没法变化,本篇随笔主要根据项目实际,介绍一下3.1版本的uploadify的控件使用,这版本目前还是最新的

  • asp.net 不用组件的URL重写(适用于较大型项目)

    先在网站根目录下建立一个config文件夹,再在此文件架下建立一个urls.config文件,这里记录url的配置信息代码如下: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <urls> <rewrite name="default" path="/default-{0}-{1}.aspx" pattern = "/default-

  • asp.net neatUpload 支持大文件上传组件

    1.在工具箱中添加Brettle.Web.NeatUpload.dll,可以看到工具箱中出现InputFile等控件. 2.复制文件夹NeatUpload到根目录. 3.拖放使用上传控件InputFile和进度条ProgressBar,添加上传文件的按钮.aspx页面代码如下: 复制代码 代码如下: <body> <form id="form1" runat="server"> <div> <upload:InputFile

  • asp.net MVC实现无组件上传图片实例介绍

    例子: 如我想上传一个图片到服务器端:asp页面 复制代码 代码如下: <form id="form1" runat="server" action="/bookIndex/fileUpLoad/(你准备处理的 ActionResult)" method="post" enctype="multipart/form-data"> <input type="file" i

  • asp.net中Word转Html的办法(不需要WORD组件)

    基本思路:把Word文件上传到服务器,读取其内容存储为Html,然后加载Html内容 1:使用Microsoft.Office.Interop.Word组件     这是比较常用的一种方式,代码就不贴出了,网上大把的例子     缺点:服务器需要装Word的组件,并且需要在服务器上设置Docm+对象的权限,如果一台服务器还好,如果项目应用到多台不同服务器,就比较繁琐了2: OpenXml API      可以将.docx(word 97-2003 不适用)转化为XML,有了XML,想转成HTM

  • MSScriptControl.ScriptControl组件的用法实例

    Microsoft(R) Script 控件使用户可以创建运行任何 ActiveX(R) scripting 引擎,例如 Microsoft(R) Visual Basic (R) Scripting Edition 或Microsoft(R) JScript(TM) 的应用程序.用户可以将任何 Automation 对象的对象模型添加到 Script 控件中,这样该对象的方法和属性就可以为 scripting 引擎所使用.通过将某个应用程序的对象模型和某个scripting 引擎加以综合,用户

  • .NET程序集引用COM组件MSScriptControl遇到问题的解决方法

    问题描述:为了在C#中执行js脚本,在一个目标平台编译为Any Cpu的.NET程序集中引用了MSScriptControl组件,在winform程序中,调用这个程序集中的执行js的方法,没有任何问题.但是在windows服务中调用却报错,报错信息如下: 在尝试过很多方法之后仍然没有解决,包括将MSScriptControl组件的引用属性"嵌入互操作类型"设置为false,如图所示: 这样子修改之后,报另外一个错误: 解决方案:MSScriptControl组件是32位的,所以需要将程

  • asp.net中Fine Uploader文件上传组件使用介绍

    最近在处理后台数据时需要实现文件上传.考虑到对浏览器适配上采用Fine Uploader. Fine Uploader 采用ajax方式实现对文件上传.同时在浏览器中直接支持文件拖拽[对浏览器版本有要求类似IE版本必须是9或是更高的IE10].在不同浏览器中提供统一用户体验.该组件基本覆盖目前所有主流浏览器.同时没有任何第三方组件依赖.相当Clear.在服务器端已经覆盖支持了ASP.NET/ColdFusion/Java/Node.js/Perl/PHP/Python. 对上传细节类似限制文件大

  • asp.net(C#)防sql注入组件的实现代码

    在服务器安全栏目里我写过一篇<破解通用Sql防注入方法>的文章中说到,一些通用的防注入方法中没有对cookie数据进行过滤,会给黑客留下可乘之机.当然我的这段代码对提交过来的cookie数据也进行了过滤. 代码: 复制代码 代码如下: using System; using System.Configuration; using System.Web; using System.Globalization; namespace JNYW.StuM.SqlInject { public clas

随机推荐