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

发布于 2024-12-21 05:57:35 字数 1583 浏览 2 评论 0原文

我需要根据路径中保存的模板发送自动电子邮件:

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

I have a requirement to send automated emails based on a template saved at path :

HostingEnvironment.MapPath("~/Content/emailTemplate/emailTemplate.oft")

I am using code below to accomplishis this, it works fine without template by using (oApp.CreateItem()), but when i use
oApp.CreateItemFromTemplate() instead of oApp.CreateItem() i get exception.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-12-28 05:57:35

//以此为例我想知道网络路径是否有问题正在解决什么错误
//你也这样吗
“~/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();
}

//use this as a example I wonder if the network path has issues being resolved what error
//are you getting as well
"~/Content/emailTemplate/emailTemplate.oft" you need to get the relative path on the network too. replace the sample below with your values and variables.

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文