zoukankan      html  css  js  c++  java
  • 发送邮件

    一、调用发送邮件

    public void SendEmail()
            {
                SendEmailParam param = new SendEmailParam();
                param.Host = "smtp.163.com";//如果是QQ邮箱则:smtp:qq.com,依次类推
                param.IsBodyHtml = true;
                param.MailBody = "<h1>哈哈哈,我是正文</h1>";
                param.MailFrom = "********@163.com";
                param.MailPwd = "*********";
                param.MailSubject = "我是Subject";
    
                //收件集合人
                param.ReceiverList = new List<string>();
                param.ReceiverList.Add("********@qq.com");
    
                //抄送人集合
                param.MailCcArray = new List<string>();
                param.MailCcArray.Add("*********@qq.com");
    
                //附件
                param.AttachmentList = new List<string>();
                param.AttachmentList.Add(@"G:11111.sql");
    
                //同步发送一个
                MailHelper.DefaultMail.SyncSendEmail(param);
                //异步发送一个
                MailHelper.DefaultMail.AsyncSendEmail(param);
            }

    二、邮件辅助

    public class MailHelper
        {
            public static readonly MailHelper DefaultMail = new MailHelper();
            private object obj = new object();
            private Thread thread = null;
    
            /// <summary>
            /// 同步发送邮件
            /// </summary>
            /// <param name="param"></param>
            /// <returns></returns>
            public bool SyncSendEmail(SendEmailParam param)
            {
                if (param.ReceiverList == null 
                    || param.ReceiverList.Count <= 0)
                {
                    new Exception("请提供收件人.");
                }
    
                MailAddress mailAddress = new MailAddress(param.MailFrom);
                MailMessage mailMessage = new MailMessage();
    
                //收件人集合
                if (param.ReceiverList!=null)
                {
                    foreach (var item in param.ReceiverList)
                    {
                        mailMessage.To.Add(item);
                    }
                }
    
                //抄送收件人集合
                if (param.MailCcArray!=null)
                {
                    foreach (var item in param.MailCcArray)
                    {
                        mailMessage.CC.Add(item);
                    }
                }
    
                //发件人地址
                mailMessage.From = mailAddress;
                //邮件标题
                mailMessage.Subject = param.MailSubject;
                //邮件标题编码
                mailMessage.SubjectEncoding = Encoding.UTF8;
                //邮件正文
                mailMessage.Body = param.MailBody;
                //邮件正文编码
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.Priority = MailPriority.High;
                mailMessage.IsBodyHtml = param.IsBodyHtml;
    
                //添加附件
                try
                {
                    if (param.AttachmentList != null && param.AttachmentList.Count > 0)
                    {
                        foreach (var item in param.AttachmentList)
                        {
                            if (!System.IO.File.Exists(item))
                            {
                                continue;
                            }
                            Attachment attachFile = new Attachment(item);
                            mailMessage.Attachments.Add(attachFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("添加附件时出错:" + ex.Message);
                }
    
                SmtpClient smtp = new SmtpClient();
                //指定发件人的邮件地址和密码以验证发件人身份
                smtp.Credentials = new System.Net.NetworkCredential(param.MailFrom, param.MailPwd);
                //设置SMTP邮件服务器
                smtp.Host = param.Host;
    
                //发送邮件
                try
                {
                    lock (obj)
                    {
                        smtp.Send(mailMessage);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
            /// <summary>
            /// 异步发送邮件
            /// </summary>
            /// <param name="param"></param>
            /// <returns></returns>
            public void AsyncSendEmail(SendEmailParam param)
            {
                thread = new Thread(new ParameterizedThreadStart(AsyncSendEmail));
                thread.Start(param);
            }
    
            private void AsyncSendEmail(object obj)
            {
                try
                {
                    this.SyncSendEmail(obj as SendEmailParam);
                }
                catch (Exception ex) { }
            }
        }

    三、实体类

    public class SendEmailParam
        {
            /// <summary>
            /// 发送者邮箱
            /// </summary>
            public string MailFrom { get; set; }
    
            /// <summary>
            /// 发送者邮箱密码
            /// </summary>
            public string MailPwd { get; set; }
    
            /// <summary>
            /// 邮件主题
            /// </summary>
            public string MailSubject { get; set; }
    
            /// <summary>
            /// 邮件正文
            /// </summary>
            public string MailBody { get; set; }
    
            /// <summary>
            /// 正文是否是HTML格式
            /// </summary>
            public bool IsBodyHtml { get; set; }
    
            /// <summary>
            /// 邮件服务器
            /// </summary>
            public string Host { get; set; }
    
            /// <summary>
            /// 收件人邮件集合
            /// </summary>
            public List<string> ReceiverList { get; set; }
    
            /// <summary>
            /// 抄送者邮件集合
            /// </summary>
            public List<string> MailCcArray { get; set; }
    
            /// <summary>
            /// 附件集合
            /// </summary>
            public List<string> AttachmentList { get; set; }
        }
  • 相关阅读:
    关于ThreadLocal的理解
    常用Linux软件安装
    Spring事务注解@Transactional失效的问题
    使用jackson转换xml格式数据进行响应
    创建简单web程序了解servlet
    JDBC
    StringBuild类
    Canlendar 日期类
    Java Date 时间类的使用
    QWeb
  • 原文地址:https://www.cnblogs.com/ITanyx/p/6492725.html
Copyright © 2011-2022 走看看