using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace OfficeUtility
{
/// <summary>
/// Outlook related operations
/// </summary>
public class OutlookUtil
{
public static void CreateAndSendMail(OutlookApp outlookApp, MailToSend mail)
{
CreateAndSendMail(outlookApp, mail.AttachFileList, mail.MailSubject, mail.ToReceiverList, mail.CCReceiverList, mail.MailBody);
}
/// <summary>
/// Create and send mail
/// </summary>
/// <param name="outlookApp">The current outlook instance</param>
/// <param name="attachedFileList">The file list will be attached to the mail</param>
/// <param name="subject">The mail subject</param>
/// <param name="originatorRecipientList">The originator of the mail</param>
/// <param name="toTypeRecipients">The recipients which will be in "To" line</param>
/// <param name="ccTypeRecipient">The recipients which will be in "CC" line</param>
/// <param name="bccTypeRecipient">The recipients which will be in "Bcc" line</param>
/// <param name="mailBody">The content of the mail</param>
public static void CreateAndSendMail(OutlookApp outlookApp, List<string> attachedFileList, string subject, List<string> originatorRecipientList, List<string> toTypeRecipients,List<string>ccTypeRecipient,List<string>bccTypeRecipient,string mailBody)
{
try
{
MailItem mail = outlookApp.OutlookAppInstance.CreateItem(OlItemType.olMailItem) as MailItem;
mail.Subject = subject;
mail.Body = mailBody;
AddressEntry currentUser = outlookApp.OutlookAppInstance.Session.CurrentUser.AddressEntry;
if (toTypeRecipients != null && toTypeRecipients.Count > 0)
{
foreach (string recipient in toTypeRecipients)
{
Recipient toRecipient = mail.Recipients.Add(recipient);
toRecipient.Type = (int)OlMailRecipientType.olTo;
}
}
if (originatorRecipientList != null && originatorRecipientList.Count > 0)
{
foreach (string recipient in originatorRecipientList)
{
Recipient originalRecipient = mail.Recipients.Add(recipient);
originalRecipient.Type = (int)OlMailRecipientType.olOriginator;
}
}
if (ccTypeRecipient != null && ccTypeRecipient.Count > 0)
{
foreach (string recipient in ccTypeRecipient)
{
Recipient ccRecipient = mail.Recipients.Add(recipient);
ccRecipient.Type = (int)OlMailRecipientType.olCC;
}
}
if (bccTypeRecipient != null && bccTypeRecipient.Count > 0)
{
foreach (string recipient in bccTypeRecipient)
{
Recipient bccRecipient = mail.Recipients.Add(recipient);
bccRecipient.Type = (int)OlMailRecipientType.olBCC;
}
}
bool isResolveSuccess = mail.Recipients.ResolveAll();
{
if (!isResolveSuccess)
{
throw new System.Exception("Warning: There's error when resolving the recipients' names");
}
}
if (attachedFileList != null)
{
try
{
foreach (string attachedFilePath in attachedFileList)
{
mail.Attachments.Add(attachedFilePath, OlAttachmentType.olByValue, Type.Missing, Type.Missing);
}
}
catch (System.Exception ex)
{
throw new System.Exception("Error: There's error when adding attach file to the mail. Exception message " + ex.Message);
}
}
mail.Send();
}
catch (System.Exception ex)
{
throw new System.Exception(string.Format("Error happens when create/send mail \"{0}\", exception message: {1}",subject, ex.Message));
}
}
/// <summary>
/// Create and send mail
/// </summary>
/// <param name="outlookApp">The current outlook instance</param>
/// <param name="attachedFileList">The file list will be attached to the mail</param>
/// <param name="subject">The mail subject</param>
/// <param name="originatorRecipientList">The originator of the mail</param>
/// <param name="toTypeRecipients">The recipients which will be in "To" line</param>
/// <param name="ccTypeRecipient">The recipients which will be in "CC" line</param>
/// <param name="mailBody">The content of the mail</param>
public static void CreateAndSendMail(OutlookApp outlookApp, List<string> attachedFileList, string subject, List<string> toTypeRecipients, List<string> ccTypeRecipient, string mailBody)
{
CreateAndSendMail(outlookApp, attachedFileList, subject, null, toTypeRecipients, ccTypeRecipient, null, mailBody);
}
/// <summary>
/// Send mail without attachment
/// </summary>
/// <param name="outlookApp">The current outlook instance</param>
/// <param name="subject">The mail subject</param>
/// <param name="toTypeRecipients">The recipients which will be in "To" line</param>
/// <param name="ccTypeRecipient">The recipients which will be in "CC" line</param>
/// <param name="mailBody">The content of the mail</param>
public static void CreateAndSendMail(OutlookApp outlookApp, string subject, List<string> toTypeRecipients, List<string> ccTypeRecipient, string mailBody)
{
CreateAndSendMail(outlookApp, null, subject, toTypeRecipients, ccTypeRecipient, mailBody);
}
}
}
Office Outlook API (二) Create and Send Mail
最新推荐文章于 2024-03-12 13:21:49 发布