.NET使用本地outlook客戶端發送郵件

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

二、封裝發送郵件方法

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


namespace ServiceMe.Apps.Business.Common.Unity
{
    /// <summary>
    /// 發送郵件幫助類
    /// </summary>
    public class MailHelper
    {    

&emsp;&emsp;&emsp;&emsp;&emsp;#region 發送郵件
        /// <summary>
        /// 發送郵件
        /// </summary>
        /// <param name="sendusermail"></param>
        /// <param name="mailtitle"></param>
        /// <param name="mailcontent"></param>
        /// <returns></returns>
        public static bool Send(string sendusermail, string mailtitle, string mailcontent)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)olApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                mailItem.To = sendusermail;
                mailItem.Subject = mailtitle;
                mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
                mailItem.HTMLBody = mailcontent;
                ((Microsoft.Office.Interop.Outlook._MailItem)mailItem).Send();
                mailItem = null;
                olApp = null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
            return true;
        }
        #endregion 
    }
}
複製代碼

三、調用示例

MailHelper.Send("xxxx@163.com", "測試郵件", "請注意,這是一封測試郵件!");
複製代碼

四、示例結果

相關文章
相關標籤/搜索