我创建了一个构建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);
}
}
尝试在对任何新创建的项进行任何修改后调用保存
方法,然后再附加它们。
此外,无需在代码中创建新的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中打开和查看该网页。