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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net.Mail;
    using System.Configuration;
    using System.Net;
    using System.Reflection;
    using System.Text;
    
    namespace SendMailMessage.Uitls
    {  
        public class EMail
        {
            // 实例化一个邮件类
            MailMessage _mailMessage = new MailMessage();
            /// <summary>
            /// 发件使用的邮箱地址
            /// </summary>
            private string _fromEmail = ConfigurationManager.AppSettings["EmailSender"].ToString();
    
            /// <summary>
            /// 发件邮箱地址密码
            /// </summary>
            private string _senderPassword = ConfigurationManager.AppSettings["EmailServer_Pwd"].ToString();
    
            /// <summary>
            /// 显示在邮件上的发件人名称
            /// </summary>
            private string _displayName = ConfigurationManager.AppSettings["EmailSenderName"].ToString();
    
            /// <summary>
            /// 发件邮箱使用的服务器
            /// </summary>
            private string _smtp = ConfigurationManager.AppSettings["EmailServer"].ToString();
    
            /// <summary>
            /// 邮件的接收者
            /// </summary>
            private string ToEmail { set; get; }
    
            /// <summary>
            /// 抄送
            /// </summary>
            private string CC { set; get; }
    
            /// <summary>
            /// 密送
            /// </summary>
            private string Bcc { set; get; }
    
            /// <summary>
            /// 邮件标题
            /// </summary>
            private string Subject { set; get; }
    
            /// <summary>
            /// 邮件正文
            /// </summary>
            private string Body { set; get; }
    
            /// <summary>
            /// 附件
            /// </summary>
            private string Attachments { set; get; }
    
    
    
            public EMail(string toEmail,  string subject, string body)
            {           
                ToEmail = toEmail;
                Subject = subject;
                Body = body;
            }
    
            public EMail(string toEmail, string cc, string bcc, string subject, string body, string attachments)
            {
    
                CC = cc;
                Bcc = bcc;
                ToEmail = toEmail;
                Subject = subject;
                Body = body;
                Attachments = attachments;
    
            }
            /// <summary>
            /// 发送email
            /// </summary>
            /// <param name="toEmail">邮件的接收者,支持群发,多个地址之间用半角逗号分开</param>    
            /// <param name="subject">主题</param>
            /// <param name="body">正文</param>    
            public static bool SendEmail(string toEmail, string subject, string body)
            {
                EMail email = new EMail(toEmail, subject, body);
                return email.Send();
            }
            
            /// <summary>
            /// 发送email
            /// </summary>
            /// <param name="toEmail">邮件的接收者,支持群发,多个地址之间用半角逗号分开</param>
            /// <param name="cc">抄送,多个地址之间用半角逗号分开</param>
            /// <param name="bcc">密送,多个地址之间用半角逗号分开</param>
            /// <param name="subject">主题</param>
            /// <param name="body">正文</param>
            /// <param name="attachments">附件地址</param>
            public static bool SendEmail(string toEmail, string cc, string bcc, string subject, string body, string attachments)
            {
                EMail email = new EMail(toEmail, cc, bcc, subject, body, attachments);
                return email.Send();
            }
    
    
            /// <summary>
            /// 邮件发送
            /// </summary>
            public bool Send()
            {
                Type type = typeof(SmtpClient);
                SmtpClient _smtpClient = new SmtpClient();           
    
                bool result = false;
    
                try
                {
                    // 将smtp的出站方式设为Network
                    _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    
                    // smtp服务器是否启用SSL加密
                    _smtpClient.EnableSsl = false;
    
                    // 指定smtp服务器地址
                    _smtpClient.Host = _smtp;
    
                    // 指定smtp服务器的端口,默认是25,如果采用默认端口,可省去
                    //smtp.Port = 25;             
    
                    // 如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
                    //smtp.UseDefaultCredentials = true;
    
                    // 如果需要认证,则用下面的方式
                    _smtpClient.Credentials = new NetworkCredential(_fromEmail, _senderPassword);
    
                    // 邮件的优先级,分为Low、Normal、 High,通常用Normal即可
                    _mailMessage.Priority = MailPriority.High;
    
                    // 收件方看到的邮件来源;第一个参数是发信人邮件地址,第二参数是发信人显示的名称,第三个参数是第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码,936是简体中文的codepage值
                    _mailMessage.From = new MailAddress(_fromEmail, _displayName, Encoding.GetEncoding(936));
    
                    // ReplyTo 表示对方回复邮件时默认的接收地址,即:你用一个邮箱发信,但却用另一个来收信,后两个参数的意义,同From
                    _mailMessage.ReplyTo = new MailAddress(_fromEmail, _displayName, Encoding.GetEncoding(936));
    
                    // 邮件的接收者,支持群发,多个地址之间用半角逗号分开
                    _mailMessage.To.Add(ToEmail);
    
                    // 抄送
                    if (!string.IsNullOrEmpty(CC))
                    {
                        _mailMessage.CC.Add(CC);
                    }
    
                    // 密送
                    if (!string.IsNullOrEmpty(Bcc))
                    {
                        _mailMessage.Bcc.Add(Bcc);
                    }
    
                    // 邮件标题
                    _mailMessage.Subject = Subject;
    
                    // 这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码
                    _mailMessage.SubjectEncoding = Encoding.GetEncoding(936);
    
                    // 邮件正文是否是HTML格式
                    _mailMessage.IsBodyHtml = true;
    
                    // 邮件正文的编码,设置不正确,接收者会收到乱码
                    _mailMessage.BodyEncoding = Encoding.GetEncoding(936);
    
                    // 邮件正文
                    _mailMessage.Body = Body;
    
                    // 附件
                    if (!string.IsNullOrEmpty(Attachments))
                    {
                        string[] attachmentsArray = Attachments.Split(',');
    
                        for (int i = 0; i < attachmentsArray.Length; i++)
                        {
                            _mailMessage.Attachments.Add(new Attachment(attachmentsArray[i]));
                        }
                    }
    
                    // 发送邮件
                    _smtpClient.Send(_mailMessage);
                    result = true;
                }
                catch (Exception ex)
                {
                    result = false;
                }
    
                return result;
            } 
        }
        
    }
  • 相关阅读:
    天大复试机试练习_003
    C++随手记--字符串转数字
    C++标准库STL 之 我觉得应该有的方法——split
    apt-get 详解&&配置阿里源
    Nginx 图文详解
    MySQL数据库管理常用命令小结
    oracle数据库备份
    SqlServer数据库备份还原步骤
    mysql数据备份与恢复
    Tomcat架构
  • 原文地址:https://www.cnblogs.com/gengaixue/p/4135978.html
Copyright © 2011-2022 走看看