csharp 发送邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 发送邮件相关的知识,希望对你有一定的参考价值。

using System;
using System.Net.Mail;
using System.Web;

namespace XXX
{
    public class SendMail
    {
        private static string sendMailADAccount = System.Configuration.ConfigurationManager.AppSettings["MailADAccount"];
        private static string sendAddress = System.Configuration.ConfigurationManager.AppSettings["MailSendAddress"];
        private static string sendPwd = System.Configuration.ConfigurationManager.AppSettings["MailSendPwd"];
        private static string sendHost = System.Configuration.ConfigurationManager.AppSettings["MailSendHost"];

        /// <summary>
        /// 邮件群发
        /// </summary>
        /// <param name="userFrom">发件人,邮箱和姓名之间以 ; 分隔,格式:""</param>
        /// <param name="userTo">收件人:多个收件人之间以 ; 分隔,格式:";"</param>
        /// <param name="userCC">抄送:多个抄送者之间以 ; 分隔,格式:";"</param>
        /// <param name="SendMessage">邮件内容</param>
        public static void GroupSend(string userTo, string userCC, Message SendMessage)
        {
            if (System.Configuration.ConfigurationManager.AppSettings["IsTestSys"] == "Y")
            {
                return;
            }
            if (String.IsNullOrEmpty(userTo))
            {
                JavascriptExtension.JavascriptAlert("收件人的邮件地址不能为空!");
                return;
            }
            try
            {
                var basePath = VirtualPathUtility.AppendTrailingSlash(System.Configuration.ConfigurationManager.AppSettings["RelativeUploadPath"]);
                var fullPath = HttpContext.Current.Server.MapPath(basePath);

                MailAddress usermail = null;
                MailMessage message = new MailMessage();
                message.From = new MailAddress(sendAddress.TrimEnd(';'), "aaaaaaaaaa");
                message.Subject = SendMessage.Title;
                message.IsBodyHtml = true;       //设置内容可以是html标签
                message.Body = SendMessage.Body;

                if (SendMessage.Attachment != null)//发送附件(多个附件的文件名称我用 | 隔开的,所以此处这样写)
                {
                    string[] arrfile = SendMessage.Attachment.Split('|');
                    for (int i = 0; i < arrfile.Length; i++)
                    {
                        if (arrfile[i].Length > 0)
                        {
                            message.Attachments.Add(new Attachment(fullPath + arrfile[i]));
                        }
                    }
                }

                SmtpClient client = new SmtpClient(sendHost);
                client.Port = 25;
                client.Credentials = new System.Net.NetworkCredential(sendMailADAccount, sendPwd);

                //定义接收人
                if (!string.IsNullOrEmpty(userTo))
                {
                    userTo = userTo.TrimEnd(';');
                    string[] userTos = userTo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < userTos.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(userTos[i]))
                        {
                            if (IsFormatEmail(userTos[i]))
                            {
                                usermail = new MailAddress(userTos[i]);
                                message.To.Add(usermail);
                            }
                        }
                    }
                }

                //定义抄送人
                if (!string.IsNullOrEmpty(userCC))
                {
                    string[] userCCs = userCC.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < userCCs.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(userCCs[i]))
                        {
                            if (IsFormatEmail(userCCs[i]))
                            {
                                usermail = new MailAddress(userCCs[i]);
                                message.CC.Add(usermail);
                            }
                        }
                    }
                }
                if (message.To.Count > 0)
                {
                    client.Send(message);
                }
            }
            catch (Exception e)
            {
                //LogHelper.Write(e);
                throw e;
            }
        }

        public static bool IsFormatEmail(string strEmail)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(strEmail, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }

    public class Message
    {
        private string _title;
        private string _body;
        private string _attachment;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        public string Attachment
        {
            get { return _attachment; }
            set { _attachment = value; }
        }
    }
}

以上是关于csharp 发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

csharp 发送邮件

csharp 致电SMO并发送电子邮件

csharp 发送电子邮件

csharp 发送电子邮件

csharp 发送电子邮件

csharp 再次发送电子邮件模板 - 拉动公司设置