.NET实现电子邮件发送实例

本来是写给码支付推送掉线信息,然后自动发提醒邮件的一个小接口,奈何服务器供应商屏蔽了25端口,465、587试了一下也没通,但是本地调试是没问题的,因为不是特别需要用到,所以暂时就不往下深究了,哪位大佬有这方面的解决方案可以直接留言告知。

        /// <summary>
        /// 发送邮件方法(阿里云限制端口)
        /// </summary>
        /// <param name="fromMial">发件人邮箱</param>
        /// <param name="toMial">收件人邮箱(多个收件人地址用";"号隔开)</param>
        /// <param name="authorizationCode">发件人授权码</param>
        /// <param name="replyTo">对方回复邮件时默认的接收地址(不设置也是可以的)</param>
        /// <param name="cCMial">//邮件的抄送者(多个抄送人用";"号隔开)</param>
        /// <param name="file_Path">附件的地址</param>
        public bool SendMail(string fromMial, string toMial, string authorizationCode, string replyTo, string cCMial, string file_Path, string massage_Title, string massage_Text)
        {
            try
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();

                //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
                mailMessage.Priority = MailPriority.Normal;

                //发件人邮箱地址。
                mailMessage.From = new MailAddress(fromMial);

                //拆分收件人邮箱地址
                List<string> ToMiallist = toMial.Split(';').ToList();
                for (int i = 0; i < ToMiallist.Count; i++)
                {
                    mailMessage.To.Add(new MailAddress(ToMiallist[i]));  //收件人邮箱地址。
                }

                if (replyTo == "" || replyTo == null)
                {
                    replyTo = fromMial;
                }

                //对方回复邮件时默认的接收地址
                mailMessage.ReplyTo = new MailAddress(replyTo);

                if (cCMial != "" && cCMial != null)
                {
                    List<string> CCMiallist = toMial.Split(';').ToList();
                    for (int i = 0; i < CCMiallist.Count; i++)
                    {
                        //邮件的抄送者,支持群发
                        mailMessage.CC.Add(new MailAddress(cCMial));
                    }
                }
                //如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
                mailMessage.SubjectEncoding = Encoding.GetEncoding(936);

                //邮件正文是否是HTML格式
                mailMessage.IsBodyHtml = false;

                //邮件标题。
                mailMessage.Subject = massage_Title;
                //邮件内容。
                mailMessage.Body = massage_Text;

                //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中  
                if (file_Path != "" && file_Path != null)
                {
                    //将附件添加到邮件
                    mailMessage.Attachments.Add(new Attachment(file_Path));
                    //获取或设置此电子邮件的发送通知。
                    mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                }

                //实例化一个SmtpClient类。
                SmtpClient client = new SmtpClient();

                #region 设置邮件服务器地址

                //在这里我使用的是163邮箱,所以是smtp.163.com,如果你使用的是qq邮箱,那么就是smtp.qq.com。
                if (fromMial.Length != 0)
                {
                    //根据发件人的邮件地址判断发件服务器地址   默认端口一般是25
                    string[] addressor = fromMial.Trim().Split(new Char[] { '@', '.' });
                    switch (addressor[1])
                    {
                        case "163":
                            client.Host = "smtp.163.com";
                            break;
                        case "126":
                            client.Host = "smtp.126.com";
                            break;
                        case "qq":
                            client.Host = "smtp.qq.com";
                            client.Port = 587;
                            break;
                        case "gmail":
                            client.Host = "smtp.gmail.com";
                            break;
                        case "hotmail":
                            client.Host = "smtp.live.com";//outlook邮箱
                            client.Port = 587;
                            break;
                        case "foxmail":
                            client.Host = "smtp.foxmail.com";
                            break;
                        case "sina":
                            client.Host = "smtp.sina.com.cn";
                            break;
                        default:
                            client.Host = "smtp.exmail.qq.com";//qq企业邮箱
                            break;
                    }
                }
                #endregion

                //使用安全加密连接。
                client.EnableSsl = true;

                //不和请求一块发送。
                client.UseDefaultCredentials = false;

                //验证发件人身份(发件人的邮箱,邮箱里的生成授权码);
                client.Credentials = new NetworkCredential(fromMial, authorizationCode);

                //如果发送失败,SMTP 服务器将发送 失败邮件告诉我  
                mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                //发送请求
                client.Send(mailMessage);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 465邮件发送
        /// </summary>
        /// <param name="to"></param>
        /// <param name="title"></param>
        /// <param name="body"></param>
        public static void SendThreadBy465(string to, string title, string body)
        {

            try
            {
                System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
                //验证  
                mmsg.Subject = title.Trim(); ;// "zhuti1";//邮件主题

                mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
                mmsg.Body = body;// "wqerwerwerwer";//邮件正文
                mmsg.BodyEncoding = Encoding.UTF8;//正文编码
                mmsg.Priority = System.Web.Mail.MailPriority.High;//优先级

                mmsg.From = "1778139233@qq.com";//发件者邮箱地址
                mmsg.To = to;//收件人收箱地址
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //登陆名  
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "邮箱账号");
                //登陆密码  
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "授权密码");
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                System.Web.Mail.SmtpMail.SmtpServer = "邮箱服务器";
                System.Web.Mail.SmtpMail.Send(mmsg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }
        /// <summary>
        /// 码支付通知接口
        /// </summary>
        /// <param name="typeID"></param>
        /// <param name="line"></param>
        /// <returns></returns>
        public ActionResult Message(int typeID, int line)
        {

            string fromMial = "发件邮箱";
            string toMial = "收件邮箱";
            string authorizationCode = "发件邮箱密码";
            string massage_Title = "码支付掉线提醒!!!";
            string massage_Text = "";

            switch (typeID)
            {
                case 1:
                    massage_Text = "支付宝:码支付账号已掉线!";
                    break;
                case 2:
                    massage_Text = "QQ钱包:码支付账号已掉线!";
                    break;
                case 3:
                    massage_Text = "微信:码支付账号已掉线!";
                    break;
                default:
                    massage_Text = "通知接口发生错误!请及时修正!";
                    break;
            }

            SendMail(fromMial, toMial, authorizationCode, null, null, null, massage_Title, massage_Text);
            SendThreadBy465(fromMial, massage_Title, massage_Text);
            return Content("1");
        }

本地调试效果如下:

THE END