Code-Helper:EmailHelper.cs

ylbtech-Code-Helper:EmailHelper.cs
1.返回顶部
1、
using System.Collections.Generic;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

namespace Sp.Common
{
    /// 
    /// 发送邮件帮助类
    /// 
    public class EmailHelper
    {
        //申明邮件操作类
        private MailMessage mail = new MailMessage();

        /// 
        /// 发送邮件,(抄送人,密送人,回复地址,所有指示标题非必须,可为null)
        /// 
        /// 发送人的邮箱
        /// 发送邮箱的授权码
        /// 收件人集合,list类型
        /// 邮件的标题,如(xxxx问题反馈)
        /// 邮件的正文部分
        /// 附件地址
        /// 抄送人集合,list类型
        /// 密送人集合,list类型
        /// 邮件指示标题,如(xxxx问题反馈)
        /// 抄送人的指示标题
        /// 密送人的指示标题
        /// 邮件的回复地址邮箱
        /// 邮件的回复地址邮箱指示标题
        /// 指定发送邮件的服务器地址或IP 
        /// 指定发送邮件端口 ,默认端口
        /// bool
        public bool sendEmail(string SenderName, string SendCode, List<string> Tosend, string title, string content, string filepath, List<string> CC, List<string> BCC, string SendTitle, string CCtitle, string BCCtitle, string Accept_reply, string acctitle, string host, int post)
        {
            bool success = false;

            //string SenderName = "xxxx@qq.com";//发送人的邮箱,后期读取配置文件中对应的数据
            //string SendCode = "xxxxx";//发送邮箱的授权码
            //bool fj = false;//判断附件是否添加成功
            try
            {
                foreach (var item in Tosend)
                {
                    mail.To.Add(item);//将多个收件人添加到队列中
                }

                mail.From = new MailAddress(SenderName, SendTitle);//将发送人添加到队列中,第二个参数表示邮件中看到的抬头部分
                mail.Subject = title;//标题
                mail.SubjectEncoding = Encoding.UTF8;//设置编码格式
                mail.Body = content;//邮件内容
                mail.BodyEncoding = Encoding.UTF8;

                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High; //发送邮件的优先等级 

                if (!string.IsNullOrWhiteSpace(Accept_reply))
                {
                    if (!string.IsNullOrWhiteSpace(acctitle))
                    {
                        mail.ReplyToList.Add(new MailAddress(Accept_reply, acctitle, Encoding.GetEncoding(936)));//回复的邮箱
                    }
                    else
                    {
                        mail.ReplyToList.Add(new MailAddress(Accept_reply));//回复的邮箱
                    }
                }

                //抄送
                if (null != CC && CC.Count > 0)
                {

                    foreach (var item in CC)
                    {

                        if (!string.IsNullOrWhiteSpace(CCtitle))
                        {
                            mail.CC.Add(new MailAddress(item, CCtitle, Encoding.GetEncoding(936)));
                        }
                        else
                        {
                            mail.CC.Add(new MailAddress(item));
                        }
                    }
                }

                if (null != BCC && BCC.Count > 0)
                {
                    //密送
                    foreach (var item in BCC)
                    {

                        if (!string.IsNullOrWhiteSpace(BCCtitle))
                        {
                            mail.Bcc.Add(new MailAddress(item, BCCtitle, Encoding.GetEncoding(936)));
                        }
                        else
                        {
                            mail.Bcc.Add(new MailAddress(item));
                        }
                    }
                }

                SmtpClient sc = new SmtpClient();//允许应用程序使用简单的邮件传输协议
                sc.EnableSsl = true;//是否SSL加密
                // sc.Host = "smtp.qq.com"; //指定发送邮件的服务器地址或IP 
                //sc.Port = 25; //指定发送邮件端口 ,默认端口

                sc.Host = host; //指定发送邮件的服务器地址或IP 
                sc.Port = post; //指定发送邮件端口 ,默认端口
                sc.Credentials = new System.Net.NetworkCredential(SenderName, SendCode); //指定登录服务器的用户名和密码(注意:这里的密码是开通上面的pop3/smtp服务提供给你的授权密码,不是你的qq密码)
                if (!string.IsNullOrEmpty(filepath) && filepath != "")
                {

                    if (!ToEnclosure(filepath))//添加附件
                    {
                        //msg = "添加附件失败";
                    }
                    else
                    {
                        sc.Send(mail);
                        //msg = "发送邮件成功";
                    }
                }

                //ToEnclosure("C:\Users\fanhuquan\Downloads\extjs-ux-master.zip");
                //sc.Send(mail);
                success = true;
            }
            catch
            {
                success = false;
            }

            return success;
        }

        /// 
        /// 发送附件,返回bool类型
        /// 
        /// 附件的路径
        private bool ToEnclosure(string filepath)
        {
            try
            {
                string[] path = filepath.Split(','); //以什么符号分隔可以自定义
                Attachment data;
                ContentDisposition disposition;
                for (int i = 0; i < path.Length; i++)
                {
                    data = new Attachment(path[i], MediaTypeNames.Application.Octet);
                    disposition = data.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);
                    mail.Attachments.Add(data);//添加附件
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/storebook/p/12684166.html