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

51次阅读

共计 2643 个字符,预计需要花费 7 分钟才能阅读完成。

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

2、封装发送邮件方法

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


namespace ServiceMe.Apps.Business.Common.Unity
{
    /// <summary>
    /// 发送邮件帮助类
    /// </summary>
    public class MailHelper
    {
        /// <summary>
        ///  邮箱用户名
        /// </summary>
        private static readonly string mailUserName = ConfigurationManager.AppSettings["MailUserName"];
        /// <summary>
        ///  邮箱密码
        /// </summary>
        private static readonly string mailUserPassword = ConfigurationManager.AppSettings["MailUserPassword"];
        /// <summary>
        /// 邮件服务器
        /// </summary>
        private static readonly string emailServer= ConfigurationManager.AppSettings["EmailServer"];
        /// <summary>
        /// 设置为 true 允许安全连接本地客户端发送邮件 ,  设置为 false 不允许允许安全连接本地客户端发送邮件 
        /// </summary>
        private static readonly string enableSsl = ConfigurationManager.AppSettings["EnableSsl"];
     
        /// <summary>
        /// 发送 EMAIL
        /// </summary>
        /// <param name="sRecipientEmail"> 收件人地址 </param>
        /// <param name="sSubject"> 主题 </param>
        /// <param name="sMessage"> 内容 </param>
        /// <param name="sSendName"> 发件人名称 </param>
        /// <returns> 发送是否成功 </returns>
        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 添加配置文件

<appSettings>
    <!-- 跨域配置 -->
    <add key="cors_allowOrigins" value="*" />
    <add key="cors_allowHeaders" value="*" />
    <add key="cors_allowMethods" value="*" />
    <add key="EmailServer" value="smtp.office365.com"/>
    <add key="EnableSsl" value="true"/>
    <add key="MailUserName" value="xiaogang.tang@medalsoft.com" />
    <add key="MailUserPassword" value="HYqdp776" />
 </appSettings>

4、调用示例

MailHelper.SendMail("xxxx@qq.com", "测试邮件", "请注意,这是一封.NET 自动使用 OutLook 发送的测试邮件!");

5、发送结果

正文完
 0