作者:明天流浪远方 | 来源:互联网 | 2023-05-19 17:06
添加引用:Microsoft.Office.Interop.OutlookusingSystem;usingSystem.Collections.Generic;usingSystem.L
添加引用:Microsoft.Office.Interop.Outlook
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace Common
{
public class MailHelper
{
public static void SendMail()
{
Outlook.Application olApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.To = "abc@163.com";
mailItem.Subject = DateTime.Now.ToString("yyyyMMdd") + "_报表";
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
string cOntent= "附件为" + DateTime.Now.ToString("yyyyMMdd") + " 数据,请查阅,谢谢!";
content = "各收件人,
请重点关注以下内容:
" + content + "
此邮件为系统自动邮件通知,请不要直接进行回复!谢谢。";
content = content + "
\r\n
Best Regards!\r\n
\r\n
\r\n
==============================================\r\n \r\n
\r\n
\r\n \r\n ===============================================";
mailItem.HTMLBody = content;
mailItem.Attachments.Add(@"c:\test.rar");
((Outlook._MailItem)mailItem).Send();
mailItem = null;
olApp = null;
}
}
}
在Microsoft Office Outlook中需要配置一个发件人的邮件账户,如果是163之类的免费邮箱,则需要设置“客户端授权密码”,然后使用这个客户端授权密码替换邮箱的登录密码来进行配置,否则是发不了邮件的。
发送成功之后如下图所示:
![](https://www.#.com/imgs/3/0/9/0/32/21711309cafed61c1d3b33fea17493b8.jpe)