WEB开发网
开发学院WEB开发ASP.NET 利用C#.net来发送邮件 阅读

利用C#.net来发送邮件

 2012-07-26 11:07:38 来源:WEB开发网   
核心提示:using System;using System.Collections.Generic;using System.Text;using System.Net.Mail;using System.Net;using System.Data;using System.ComponentModel;namespace I
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; //"smtp.sina.com", your smtp server here10.128.1.1          
            ////用户名凭证      
            if (!string.IsNullOrEmpty(strUserName) && !string.IsNullOrEmpty(strPwd))
                smtpClient.Credentials = new System.Net.NetworkCredential(strUserName, strPwd);

            #endregion

            bool isSuccess = true;
            try
            {
                //发送邮件
                if (isAsync)
                {
                    smtpClient.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);
                    smtpClient.SendAsync(myMail, myMail);
                }
                else
                    smtpClient.Send(myMail);

            }
            catch (Exception e)
            {
                isSuccess = false;
                //log.Error(e.Message);
            }
            return isSuccess;
        }


        static void SendMailCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MailMessage mailMsg = (MailMessage)e.UserState;
            string subject = mailMsg.Subject as string;
            if (e.Cancelled) // 邮件被取消
            {
                Console.WriteLine(MailAddressToString(mailMsg) + subject + " 被取消。");
                Console.WriteLine(" 被取消。");
                //log.Info(MailAddressToString(mailMsg) + subject + "被取消。");
            }
            if (e.Error != null)
            {
                Console.WriteLine("错误:" + e.Error.ToString());
                //log.Warn("错误:" + e.Error.ToString());
            }
            else
            {
                Console.WriteLine(MailAddressToString(mailMsg) + "发送完成。");
                //log.Info(MailAddressToString(mailMsg) + "发送完成。");
            }
        }

        private static string MailAddressToString(MailMessage mailMsg)
        {
            StringBuilder sb = new StringBuilder();
            foreach (MailAddress addr in mailMsg.To)
            {
                sb.Append(addr.Address + " ");
            }
            return sb.ToString();
        }

        

    }


}

抄送者通常为企业主管的Email
第一个方法为邮件服务器不用校验用户名密码的,现在很少使用
通常调用第二个方法来发送邮件
你的邮箱需要开通企业邮箱服务功能,才能发送邮件,不同的邮件提供商的企业邮箱发送频率是有限制的!

Tags:利用 net 发送

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接