Office Outlook API (二) Create and Send Mail

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值