C#调用Outlook API -- 发送邮件

发表于:2013-12-11 10:20

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Lostin_Zhu    来源:51Testing软件测试网采编

  鉴于项目需要,最近对Outlook API进行了研究。我们需要实现通过内部工具(C#实现)调用Outlook来定期发送邮件,邮件部分内容需人工撰写,部分内容是工具自动生成的html格式的报告。大致需求如下:
  自动填写邮件主题,收件人,默认内容
  嵌入生成的html报告
  显示邮件,经发件人确认修改后手动发送
  具体解决方案如下:
  1. 添加引用。
  工程 Reference 右键 -> COM tab -> Microsoft Outlook 12.0 Object Library.
  此时可见引用列表中多了“System.Office.Interop.Outlook”.
  在程序文件头部添加“using Microsoft.Office.Interop.Outlook;”.
  2. 发送邮件。
# generate email automatically
private string GenerateEmail()
{
# default content of mail, sender will modify and verify before sending out
string hello = "Hi,<br><br>This is an example for sending automatic email via outlook.<br><br>Thanks,";
# html report that is gennerated automatically
string report = "<html>report content.</html>";
ApplicationClass app = new Microsoft.Office.Interop.Outlook.ApplicationClass();
MailItem mail = (MailItem)app.CreateItem(OlItemType.olMailItem); # create mail object
mail.SendUsingAccount = app.Session.Accounts[1];    # sender
string date = DateTime.Today.Month + "-" + DateTime.Today.Day + "-" + DateTime.Today.Year;    # current date,format “7-10-2013”
mail.Subject = "[" + date + ", pass] Smoke Test Report";    # email subject
mail.To = "to_1@***.com";    # To list
mail.CC = "cc_1@***.com;cc_2@***.com;";    # cc list
mail.BodyFormat = OlBodyFormat.olFormatHTML;    # email is in html format
mail.HTMLBody = hello + report + ReadSignature();    # email content
mail.Display();    # popup email interface before sending
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号