1. 程式人生 > >C#發送郵件

C#發送郵件

style inner play 調用 eat combine roo splay tps

郵件發送類:

技術分享
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("[email protected]", "Ben Miller"); //MailAddress to = new MailAddress("[email protected]", "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("[email protected]", "Ben Miller"); //MailAddress to = new MailAddress("[email protected]", "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);
        }

C#發送郵件