繁体   English   中英

通过Outlook C#通过预定义模板发送电子邮件

[英]Email on predefined template via outlook C#

我需要根据保存在path的模板发送自动电子邮件:

HostingEnvironment.MapPath(“〜/ Content / emailTemplate / emailTemplate.oft”)

我正在使用下面的代码来完成此操作,它可以通过使用(oApp.CreateItem())在没有模板的情况下正常工作,但是当我使用oApp.CreateItemFromTemplate()而不是oApp.CreateItem()时,我得到了异常。

public static void CreateMessageWithAttachment(
          string invoiceNumber, string recipient,  string messageBody)
{

    Outlook.Application oApp = new Outlook.Application();
    Outlook.Folders folder = oApp.Session.GetDefaultFolder(
                               Outlook.OlDefaultFolders.olFolderDrafts) 
                                as Outlook.Folders;
    Outlook.MailItem email = oApp.CreateItemFromTemplate(
                               HostingEnvironment.MapPath(
                                "~/Content/emailTemplate/emailTemplate.oft"), folder)
                                   as Outlook.MailItem;

    email.Recipients.Add(recipient);
    email.Subject = "Invoice # " + invoiceNumber;

    {
      string fileName = invoiceNumber.Trim();
      string filePath = HostingEnvironment.MapPath("~/Content/reports/");
      filePath = filePath + fileName + ".pdf";
      fileName += ".pdf";
      int iPosition = (int)email.Body.Length + 1;
      int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
      Outlook.Attachment oAttach = email.Attachments.Add(
                           filePath, iAttachType, iPosition, fileName);
    }

    email.Display();
    ////..uncomment below line to SendAutomatedEmail emails atomaticallly
    ////((Outlook.MailItem)email).Send(); 
}

//以该示例为例,我想知道网络路径是否已解决问题,这是什么错误//您是否还需要“〜/ Content / emailTemplate / emailTemplate.oft”来获取网络上的相对路径。 将下面的示例替换为您的值和变量。

private void CreateItemFromTemplate()
{
    Outlook.Folder folder =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
    Outlook.MailItem mail =
        Application.CreateItemFromTemplate(
        @""~/Content/emailTemplate/emailTemplate.oft", folder) as Outlook.MailItem;
    mail.Subject = "Congratulations";
    mail.Save();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM