C#在outlook里创建一封邮件到草稿箱

时间:2022-11-27 20:21:33

原文:C#在outlook里创建一封邮件到草稿箱

1.引用Microsoft.Office.Interop.Outlook.dll

2.  实现代码

 public static int SendToDraft(List<string> to, List<string> cc, List<string> bcc, string subject, string content, List<string> attachments, ref string errorMessage)
{
int result = -;
try
{
StringBuilder sbattachment = new StringBuilder();
Application objOutlook = new Application();
NameSpace oNameSpace = objOutlook.GetNamespace("MAPI");
MAPIFolder folder = oNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderDrafts);
MailItem item = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));
item.To = GetString(to);
item.CC = GetString(cc);
item.BCC = GetString(bcc);
item.Subject = subject;
item.HTMLBody = content;
new Log("send email outlook ").Write("");
//添加附件
if (attachments != null && attachments.Count > )
{
for (int i = ; i < attachments.Count; i++)
{
item.Attachments.Add(@attachments[i]);
}
}
item.SaveSentMessageFolder = folder;
item.Save();
result = ;
}
catch (System.Exception ex)
{
result = -;
errorMessage = ex.Message.ToString();
}
return result;
}