当前位置: 首页 > 知识库问答 >
问题:

C#。净额4.7。2:将outlook对象嵌入为电子邮件附件

解河
2023-03-14

我创建了一个构建html字符串作为电子邮件正文的代码。然而,我希望这封电子邮件是我的一般和基础电子邮件,我在第一个地方创建的附件。下面的代码实际上是有效的,但在最后,当我打开我的普通电子邮件时,我将第二封电子邮件作为附件,但当我单击它时,附件中的电子邮件是Black电子邮件。尽管我创建了html主体,但结果仍然如此。html字符串在最后一个方法“AddMessageAsAttachment”的末尾消失。任何帮助都将不胜感激。。。非常感谢。

using Microsoft.Office.Interop.Outlook;

foreach (var positionlist in result)
{
    string salesEmailAddress = positionlist.Key;

    //Create the general email to the salesman
    Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Application();
    MailItem generalMail = application.CreateItem(Outlook.OlItemType.olMailItem) as MailItem;
    generalMail.Body = "Hello, Please find your quarterly reports";
    generalMail.Subject = $"Quarterly Report";
    generalMail.To = salesEmailAddress;

    foreach (DataRow row in positionlist)
    {
        MailItem mailToAdd = CreateSecondaryEmail(row, "EN", "P:/My_path/Report_EN.oft", @"P:\Another_path\SecondaryEmailAttachment.pdf");
        Globale.AddMessageAsAttachment(generalMail, mailToAdd);
    }

    private MailItem CreateSecondaryEmail(DataRow row, string lang, string template, string attachement)
    {
        string clientNumber = row["account_nbr"];
        string currency = row["Ccy"];

        html += $@"<html>
                    <div style='font-family: Haboro Contrast Ext Thin;font-size: 15,5'>
                        <p style=''></p><BR>
                        <span style=''>{clientNumber}</span><BR>
                        <span style=''> {currency}</span><BR>            
                    </div>";

        html += "</html>";
        MailItem mailPosition = Globale.CreateItemFromTemplate(html, email, template, factsheet);

        return mailPosition;
    }
}

“CreateItemFromTemplate”实际上是从一个模板链接到第二封电子邮件的正文和一份快速pdf报告(参数“attachement”),该报告将附加:

public static Outlook.MailItem CreateItemFromTemplate(string html, string emailaddress, string template, string attachement = null, string client_number = null)
    {

        Outlook.Application application = new Outlook.Application();
        Outlook.MailItem mail = application.CreateItemFromTemplate(template) as Outlook.MailItem;
        mail.HTMLBody = mail.HTMLBody.Replace("#BODY", html);
        DateTime dt = DateTime.Now;
        string date = $"Data as of  {dt.Day}.{dt.Month}.{dt.Year}";
        mail.HTMLBody = mail.HTMLBody.Replace("#DATE",date);
        mail.Subject = $"Quarterly Report of the client {client_number}";
        mail.To = emailaddress;
        if (attachement != null)
        {
            mail.Attachments.Add(attachement);
        }
        return mail;     
    }

最后,我创建了一个“Globale”类,其中包含一个方法,该方法将检索由上述“CreateItemFromTemplate”方法生成的第二封电子邮件(mailPosition),并将其作为附件放在普通电子邮件中。

public static void AddMessageAsAttachment(Outlook.MailItem mailContainer,Outlook.MailItem mailToAttach)
    {
        Outlook.Attachments attachments = null;
        Outlook.Attachment attachment = null;
        try
        {
            attachments = mailContainer.Attachments;
            attachment = attachments.Add(mailToAttach,
               Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail");
            mailContainer.Save();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        finally
        {
            if (attachment != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(attachment);
            if (attachments != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(attachments);
        }
    }

共有1个答案

胡泓
2023-03-14

尝试在对任何新创建的项进行任何修改后调用保存方法,然后再附加它们。

此外,无需在代码中创建新的Outlook应用程序实例。例如,我注意到您文章开头的以下代码:

//Create the general email to the salesman
    Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Application();
    MailItem generalMail = application.CreateItem(Outlook.OlItemType.olMailItem) as MailItem;
    generalMail.Body = "Hello, Please find your quarterly reports";

CreateItemFromTemplate方法中,我看到:

Outlook.Application application = new Outlook.Application();
Outlook.MailItem mail = application.CreateItemFromTemplate(template) as Outlook.MailItem;
mail.HTMLBody = mail.HTMLBody.Replace("#BODY", html);

最后,我建议附加网页,而不是创建项目并附加它们。可以在Outlook中打开和查看该网页。

 类似资料:
  • 我的VBA代码(工作): 谢谢你在这方面的帮助,蒂姆

  • 我已经发布了一个与此相关的问题(电子邮件中的HTML嵌入式图像未显示),并更新了我的脚本以包含cid,但现在我的图像是附加的,而不是内联的。我不确定如何在outlook对象中使用内联图像。 然后创建一个outlook对象并将其与附件一起发送。 它是附加的,没有任何问题,但不嵌入内联。有谁能帮上忙吗。 我没有smtp服务器的详细信息,所以我不能使用smtp客户端。所以我试着像下面这样使用这个类Sys

  • 问题内容: 我目前有一个程序,可以从列表中随机选择引号并通过电子邮件发送。我现在也尝试在电子邮件中嵌入图片。我遇到了一个问题,可以附加电子邮件,但报价不再起作用。我已经在网上进行了研究,但解决方案对我不起作用。请注意,我使用的是Python 3.2.2。 任何指导将不胜感激。 从上面的代码可以看出,我尝试了不同的方法(引用#) 问题答案: 您正在费力地在中构造有效的MIME消息,然后放弃它并发送一

  • 问题内容: 我正在尝试发送包含嵌入式gif图像的多部分/相关html电子邮件。该电子邮件是使用Oracle PL / SQL生成的。我的尝试失败了,图片显示为红色X(在Outlook 2007和yahoo邮件中) 我已经发送HTML电子邮件已有一段时间了,但是我现在的要求是在电子邮件中使用多个gif图像。我可以将它们存储在我们的一台Web服务器上并仅链接到它们,但是许多用户的电子邮件客户端不会自动

  • 问题内容: 我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作,但没有附件。 但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例GitHub sendgrid, 所以我一直在尝试直到它起作用为止,我缩小了一些异常和响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),这是我的代码发送

  • 这似乎很难,而且 我的电子邮件内容是html格式的,所以我必须首先将RTF转换为html格式。