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

    邮件发送类:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Configuration;
    using System.Web;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    
    namespace Maticsoft.Common
    {
        /// <summary>
        ///  邮件发送类
        /// </summary>
        public class MailSender
        {
    
            public static void Send(string tomail, string bccmail, string subject, string body, params string[] files)
            {
                Send(SmtpConfig.Create().SmtpSetting.Sender, tomail, bccmail, subject, body, true, Encoding.Default, true, files);
            }        
    
    
            public static void Send(string frommail, string tomail,string bccmail, string subject,
                            string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
            {            
                Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.UserName, SmtpConfig.Create().SmtpSetting.Password, frommail,
                    tomail,"", bccmail, subject, body, isBodyHtml, encoding, isAuthentication,false, files);
            }
    
    
            //直接调用该方法发送邮件,可抄送,密送,逗号分隔多人
            public static void Send(string server,string username,string password, string frommail, string tomail, string ccmail,string bccmail, string subject,
                            string body, bool isBodyHtml, Encoding encoding, bool isAuthentication,bool isSsl, params string[] files)
            {
    
    
                SmtpClient smtpClient = new SmtpClient(server);
                //MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
                //MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
                MailMessage message = new MailMessage(frommail, tomail);
    
                if (bccmail.Length > 1)
                {
                    string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(bccmail);
                    foreach (string m in maillist)
                    {
                        if (m.Trim() != "")
                        {
                            MailAddress bcc = new MailAddress(m.Trim());
                            message.Bcc.Add(bcc);
                        }
                    }
                }
                if (ccmail.Length > 1)
                {
                    string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(ccmail);
                    foreach (string m in maillist)
                    {
                        if (m.Trim() != "")
                        {
                            MailAddress cc = new MailAddress(m.Trim());
                            message.CC.Add(cc);
                        }
                    }
                }            
                message.IsBodyHtml = isBodyHtml;
                message.SubjectEncoding = encoding;
                message.BodyEncoding = encoding;
    
                message.Subject = subject;
                message.Body = body;
    
                message.Attachments.Clear();
                if (files != null && files.Length != 0)
                {
                    for (int i = 0; i < files.Length; ++i)
                    {
                        Attachment attach = new Attachment(files[i]);
                        message.Attachments.Add(attach);
                    }
                }
    
                if (isAuthentication == true)
                {
                    smtpClient.Credentials = new NetworkCredential(username, password);
                    smtpClient.EnableSsl =isSsl;
                }
                smtpClient.Send(message);
                message.Attachments.Dispose();
    
            }
    
            /// <summary>
            /// 重载发送邮件的方法(增加邮件的回复地址)
            /// </summary>
            /// <param name="server"></param>
            /// <param name="username"></param>
            /// <param name="password"></param>
            /// <param name="frommail"></param>
            /// <param name="tomail"></param>
            /// <param name="ccmail"></param>
            /// <param name="bccmail"></param>
            /// <param name="replymail"></param>
            /// <param name="subject"></param>
            /// <param name="body"></param>
            /// <param name="isBodyHtml"></param>
            /// <param name="encoding"></param>
            /// <param name="isAuthentication"></param>
            /// <param name="isSsl"></param>
            /// <param name="files"></param>
            public static void Send(string server,string username,string password, string frommail, string tomail, string ccmail,string bccmail, string replymail,string subject,
                            string body, bool isBodyHtml, Encoding encoding, bool isAuthentication,bool isSsl, params string[] files)
            {
    
    
                SmtpClient smtpClient = new SmtpClient(server);
                //MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
                //MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
                MailMessage message = new MailMessage(frommail, tomail);
    
                if (bccmail.Length > 1)
                {
                    string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(bccmail);
                    foreach (string m in maillist)
                    {
                        if (m.Trim() != "")
                        {
                            MailAddress bcc = new MailAddress(m.Trim());
                            message.Bcc.Add(bcc);
                        }
                    }
                }
                if (ccmail.Length > 1)
                {
                    string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(ccmail);
                    foreach (string m in maillist)
                    {
                        if (m.Trim() != "")
                        {
                            MailAddress cc = new MailAddress(m.Trim());
                            message.CC.Add(cc);
                        }
                    }
                }
                if (replymail.Length > 1)
                {
                    string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(replymail);
                    foreach (string m in maillist)
                    {
                        if (m.Trim() != "")
                        {
                            MailAddress cc = new MailAddress(m.Trim());
                            message.ReplyToList.Add(cc);
                        }
                    }
                }
                message.IsBodyHtml = isBodyHtml;
                message.SubjectEncoding = encoding;
                message.BodyEncoding = encoding;
    
                message.Subject = subject;
                message.Body = body;
    
                message.Attachments.Clear();
                if (files != null && files.Length != 0)
                {
                    for (int i = 0; i < files.Length; ++i)
                    {
                        Attachment attach = new Attachment(files[i]);
                        message.Attachments.Add(attach);
                    }
                }
    
                if (isAuthentication == true)
                {
                    smtpClient.Credentials = new NetworkCredential(username, password);
                    smtpClient.EnableSsl = true;
                }
                smtpClient.Send(message);
                message.Attachments.Dispose();
    
            }
    
            public static void Send(string recipient, string subject, string body)
            {
                Send( SmtpConfig.Create().SmtpSetting.Sender, recipient,"", subject, body, true, Encoding.Default, true, null);
            }
    
            public static void Send(string Recipient, string Sender, string Subject, string Body)
            {
                Send(Sender, Recipient, "",Subject, Body, true, Encoding.UTF8, true, null);
            }
                    
        }
    
        public class SmtpSetting
        {
            private string _server;
    
            public string Server
            {
                get { return _server; }
                set { _server = value; }
            }
            private bool _authentication;
    
            public bool Authentication
            {
                get { return _authentication; }
                set { _authentication = value; }
            }
            private string _username;
    
            public string UserName
            {
                get { return _username; }
                set { _username = value; }
            }
            private string _sender;
    
            public string Sender
            {
                get { return _sender; }
                set { _sender = value; }
            }
            private string _password;
    
            public string Password
            {
                get { return _password; }
                set { _password = value; }
            }
        }
    
        public class SmtpConfig
        {
            private static SmtpConfig _smtpConfig;
            private string ConfigFile
            {
                get
                {
                    string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
                    if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
                    {
                        configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
                    }
                    else
                    {
                        if (!Path.IsPathRooted(configPath))
                            configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
                        else
                            configPath = Path.Combine(configPath, "SmtpSetting.config");
                    }
                    return configPath;
                }
            }
            public SmtpSetting SmtpSetting
            {
                get
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(this.ConfigFile);
                    SmtpSetting smtpSetting = new SmtpSetting();
                    smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
                    smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
                    smtpSetting.UserName = doc.DocumentElement.SelectSingleNode("User").InnerText;
                    smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
                    smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
    
                    return smtpSetting;
                }
            }
            private SmtpConfig()
            {
    
            }
            public static SmtpConfig Create()
            {
                if (_smtpConfig == null)
                {
                    _smtpConfig = new SmtpConfig();
                }
                return _smtpConfig;
            }
        }
    }
    View Code

    分割多个收件人:

    #region 获取用逗号分割后的字符数组
            /// <summary>
            /// 获取用逗号分割后字符数组
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string[] GetStrArray(string str)
            {
                return str.Split(new Char[] { ',' });
            }
            #endregion
    View Code

    调用发邮件的方法:(示例是企业qq邮箱)

     public void SendEmail(string message)
            {
                var ToEmail = System.Configuration.ConfigurationManager.AppSettings["toEmail"].ToString();
                var csEmail = System.Configuration.ConfigurationManager.AppSettings["csEmail"].ToString();
                var msEmail = System.Configuration.ConfigurationManager.AppSettings["msEmail"].ToString();
                Maticsoft.Common.MailSender.Send("smtp.exmail.qq.com", "发件人", "发件密码", "发件人", ToEmail, csEmail, msEmail, "邮件主题", message, false, Encoding.UTF8, true, true, null);
            }
  • 相关阅读:
    linux办公软件的使用和病毒防范
    需要了解的基本礼仪素养
    遗留问题
    shell基本命令
    shell编程
    遇到过得问题
    mac电脑操作
    Linux编程
    BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)
    BZOJ 2653 middle (可持久化线段树+中位数+线段树维护最大子序和)
  • 原文地址:https://www.cnblogs.com/SmilePastaLi/p/7159520.html
Copyright © 2011-2022 走看看