.NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件

1、添加Microsoft.Office.Interop.Outlook引用

2、封装发送邮件方法

using System;
using System.Configuration;
using System.Net.Mail;


namespace ServiceMe.Apps.Business.Common.Unity
{
    /// 
    /// 发送邮件帮助类
    /// 
    public class MailHelper
    {
        /// 
        ///  邮箱用户名
        /// 
        private static readonly string mailUserName = ConfigurationManager.AppSettings["MailUserName"];
        /// 
        ///  邮箱密码
        /// 
        private static readonly string mailUserPassword = ConfigurationManager.AppSettings["MailUserPassword"];
        /// 
        /// 邮件服务器
        /// 
        private static readonly string emailServer= ConfigurationManager.AppSettings["EmailServer"];
        /// 
        /// 设置为true允许安全连接本地客户端发送邮件 ,  设置为false不允许允许安全连接本地客户端发送邮件 
        /// 
        private static readonly string enableSsl = ConfigurationManager.AppSettings["EnableSsl"];
     
        /// 
        /// 发送EMAIL
        /// 
        /// 收件人地址
        /// 主题
        /// 内容
        /// 发件人名称
        /// 发送是否成功
        public static bool SendMail(string sRecipientEmail, string sSubject, string sMessage)
        {
            try
            {
                //邮件对象
                MailMessage emailMessage;

                //smtp客户端对象
                SmtpClient client;

                string sSenderEmail = mailUserName;
                emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
                emailMessage.IsBodyHtml = true;
                emailMessage.SubjectEncoding = System.Text.Encoding.Default;
                emailMessage.BodyEncoding = System.Text.Encoding.Default;
                //加入
                emailMessage.Headers.Add("X-Priority", "3");
                emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
                emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
                emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
                emailMessage.Headers.Add("ReturnReceipt", "1");

                //邮件发送客户端
                client = new SmtpClient();
                //邮件服务器及帐户信息
                client.Host = emailServer;
                //client.Host = "smtp.163.com";
                //client.Port = 465;
                //client.EnableSsl = true;
                System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();

                //web.config中读取邮件服务器用户名和密码信息
                Credential.UserName = mailUserName;
                Credential.Password = mailUserPassword;
                client.Credentials = Credential;
                client.EnableSsl =Convert.ToBoolean(enableSsl);
                //发送邮件
                client.Send(emailMessage);
            }
            catch(Exception ex)
            {
                //错误处理待定
                Log4NetHelper.WriteError($"发送邮件错误信息:{ex.Message},\r\n堆栈信息:{ex.StackTrace}");
                return false;
            }
            return true;
        }
    }
}

3、config添加配置文件

 
    
    "cors_allowOrigins" value="*" />
    "cors_allowHeaders" value="*" />
    "cors_allowMethods" value="*" />
    "EmailServer" value="smtp.office365.com"/>
    "EnableSsl" value="true"/>
    "MailUserName" value="[email protected]" />
    "MailUserPassword" value="HYqdp776" />
 

4、调用示例

 MailHelper.SendMail("[email protected]", "测试邮件", "请注意,这是一封.NET自动使用OutLook发送的测试邮件!");

5、发送结果

.NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件_第1张图片

 

你可能感兴趣的:(.NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件)