深入Lumisoft.NET组件与.NET API实现邮件发送功能的对(3)

//附件
            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对象即可。

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgxzd.html