.netl发送邮件

.net1.1中,web应该可以使用System.Web.Mail发送邮件。.net2.0后.net framework中添加了一个System.Net.Mail的命名空间,用于发送邮件。
使用System.Web.Mail发送邮件
public class SendMail
    {
        public static void Send(string mailFrom, string maiFromlAccount,string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent, IList<MailAttachment> mailAttachments,System.Text.Encoding encoding,MailFormat format)
        {
            MailMessage message = new MailMessage();
            //定义SMTP邮件服务器需要身份认证
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //认证的用户名
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", maiFromlAccount);
            //认证密码
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailFromPwd);
            string sMailTo = "";
            foreach (string s in mailTo)
            {
                sMailTo += s + ";";
            }
            if (sMailTo == "")
            {
                throw new Exception("接收邮件不可以为空");
            }
            sMailTo = sMailTo.TrimEnd(';');
            if (mailFrom.Trim() == "")
            {
                throw new Exception("发送邮件不可以为空");
            }
            message.From = mailFrom;
            message.To = sMailTo;
            if (mailCC.Count > 0)
            {
                string sMailCC = "";
                foreach (string s in mailCC)
                {
                    sMailCC += s + ";";
                }
                sMailCC = sMailCC.TrimEnd(';');
                message.Cc = sMailCC;
            }
            if (mailBCC.Count > 0)
            {
                string sMailBCC = "";
                foreach (string s in mailBCC)
                {
                    sMailBCC += s + ";";
                }
                sMailBCC = sMailBCC.TrimEnd(';');
                message.Bcc = sMailBCC;
            }
            message.Subject = mailTitle;
            message.Body = mailContent;
            message.BodyEncoding = encoding;   //邮件编码
            message.BodyFormat = format;       //内容格式 html还是text
            message.Priority = MailPriority.High;  //设置发送的优先集
            //附件
            foreach (MailAttachment att in mailAttachments)
            {
                message.Attachments.Add(att);
            }
            SmtpMail.SmtpServer = mailSmtpServer;
            SmtpMail.Send(message);
        }

        public static void SendText(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
        {
            List<MailAttachment> attList = new List<MailAttachment>();
            Send(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, MailFormat.Text);
        }

        public static void SendHTML(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
        {
            List<MailAttachment> attList = new List<MailAttachment>();
            Send(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, MailFormat.Html);
        }
    }

使用System.Net.Mail发送邮件
public class NetSendMail
    {
        public static void MailSend(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent, IList<string> mailAttachments, System.Text.Encoding encoding, bool isBodyHtml)
        {
            MailMessage message = new MailMessage();
            if (mailFrom.Trim() == "")
            {
                throw new Exception("发送邮件不可以为空");
            }
            message.From = new MailAddress(mailFrom);
            if (mailTo.Count <= 0)
            {
                throw new Exception("接收邮件不可以为空");
            }
            foreach (string s in mailTo)
            {
                message.To.Add(new MailAddress(s));
            }
            if (mailCC.Count > 0)
            {
                foreach (string s in mailCC)
                {
                    message.CC.Add(new MailAddress(s));
                }
            }
            if (mailBCC.Count > 0)
            {
                foreach (string s in mailBCC)
                {
                    message.Bcc.Add(new MailAddress(s));
                }
            }
            message.Subject = mailTitle;
            message.Body = mailContent;
            message.BodyEncoding = encoding;   //邮件编码
            message.IsBodyHtml = isBodyHtml;      //内容格式是否是html
            message.Priority = MailPriority.High;  //设置发送的优先集
            //附件
            foreach (string att in mailAttachments)
            {
                message.Attachments.Add(new Attachment(att));
            }
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = mailSmtpServer;
            smtpClient.Credentials = new NetworkCredential(maiFromlAccount, mailFromPwd);
            smtpClient.Timeout = 1000;
            smtpClient.EnableSsl = false;        //不使用ssl连接
            smtpClient.Send(message);
        }

        public static void MailSendText(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
        {
            List<string> attList = new List<string>();
            MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, false);
        }

        public static void MailSendHTML(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
        {
            List<string> attList = new List<string>();
            MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, true);
        }

你可能感兴趣的:(发送邮件,net发送邮件)