日期:2014-05-18  浏览次数:21147 次

C#.net发送邮件
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Data;
using System.ComponentModel;



namespace ITTS_Express.Contact
{
    public class MailHelper
    {
        //private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(MailHelper));
        /// <summary>
        /// 發送郵件
        /// </summary>
        /// <param name="strFrom">收件人Email Address</param>
        /// <param name="lstTo">發件人Email Address列表</param>
        /// <param name="strSubject">郵件標題</param>
        /// <param name="strBody">郵件内容</param>
        /// <param name="isBodyHtml">是否是HTML格式</param>
        /// <param name="strSmtpServer">SMTP 郵件服務器</param>
        /// <param name="lstAttachment_FullPath">附件路徑列表</param>
        /// <returns>是否發送成功</returns>
        public static bool SendMail(string strFrom, List<string> lstTo, List<string> listCC, string strSubject,
            string strBody, bool isBodyHtml, string strSmtpServer,
            List<string> lstAttachment_FullPath, bool isAsync)
        {
            return SendMail(strFrom, lstTo, listCC, strSubject, strBody, isBodyHtml, strSmtpServer, null, null, lstAttachment_FullPath, isAsync);
        }

        /// <summary>
        /// 發送郵件
        /// </summary>
        /// <param name="strFrom">收件人Email Address</param>
        /// <param name="lstTo">發件人Email Address列表</param>
        /// <param name="lstCC">抄送者Email地址</param>
        /// <param name="strSubject">郵件標題</param>
        /// <param name="strBody">郵件内容</param>
        /// <param name="isBodyHtml">是否是HTML格式</param>
        /// <param name="strSmtpServer">SMTP 郵件服務器</param>
        /// <param name="strUserName">用戶名</param>
        /// <param name="strPwd">密碼</param>
        /// <param name="lstAttachment_FullPath">附件路徑列表</param>
        /// <returns>是否發送成功</returns>
        public static bool SendMail(string strFrom, List<string> lstTo, List<string> lstCC, string strSubject,
            string strBody, bool isBodyHtml, string strSmtpServer,
            string strUserName, string strPwd, List<string> lstAttachment_FullPath, bool isAsync)
        {
            #region ==== 创建电子邮件 ====

            MailMessage myMail = new MailMessage();
            //发件人
            myMail.From = new MailAddress(strFrom);
            //收件人
            foreach (string item in lstTo)
                myMail.To.Add(item);
            if (lstCC != null && lstCC.Count > 0)
            {
                foreach (string item in lstCC)
                    myMail.CC.Add(item);
            }
            //主题
            myMail.Subject = strSubject;
            //内容
            myMail.Body = strBody;
            //正文编码
            myMail.BodyEncoding = System.Text.Encoding.UTF8;
            //设置为HTML格式
            myMail.IsBodyHtml = isBodyHtml;
            //优先级
            myMail.Priority = MailPriority.Normal;
            //发送附件
            if (lstAttachment_FullPath != null && lstAttachment_FullPath.Count > 0)
            {
                foreach (string attachment_FullPath in lstAttachment_FullPath)
                    myMail.Attachments.Add(new Attachment(attachment_FullPath));
            }

            #endregion

            #region ==== 配置支持Stmp协议的客户端 ====

            SmtpClient smtpClient = new SmtpClient();
            //发送方式
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            //smtp服务器
            smtpClient.Host = strSmtpServer; //&quo