欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-20 17:21:09
1、添加Microsoft.Office.Interop.Outlook引用 2、封装发送邮件方法 3、config添加配置文件 4、调用示例 5、发送结果 ......

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、发送结果

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