Microsoft OfficeWindows VistaWindows 2003Visual Studio 2005Windows XP.NET 2.0C# 2.0中级开发Visual StudioWindows.NETC#
以编程方式转发电子邮件消息(含内嵌图片)
在使用 VSTO 2005 时,我需要程序化地转发 Outlook 2007 电子邮件消息。默认情况下,所选电子邮件中的内联图片将作为附件添加到转发的电子邮件中。这段示例代码将帮助您程序化地转发带有内联图片的电子邮件消息。
引言
这段示例代码对使用 VSTO 2005 程序化地转发带有内联图片的 Outlook 2007 电子邮件消息的开发人员来说非常有用。
背景
当我尝试使用 MailItem.Forward()
方法转发电子邮件时,由于我实现了自定义 Outlook 表单,所以它没有正常工作。而且,默认情况下,所选消息中的内联图片将作为附件添加到转发消息中。使用这段代码,您将获得一个解决方案,可以转发任何带有内联图片的电子邮件消息。
使用代码
在这个示例应用程序中,我遵循以下步骤:
- 获取当前右键单击的电子邮件(所选电子邮件)。
- 创建一个新的 Outlook 邮件(转发邮件)。
- 将所选电子邮件的所需属性分配给转发邮件。
- 修剪所选电子邮件的
HTMLBody
字符串,以便内联正文图片在转发邮件中显示为图片。(否则,所选电子邮件中的内联图片将作为附件添加到转发邮件中。) - 在新窗口中打开转发邮件。
下面显示了实现上述步骤的 ForwardSelectedMessage()
方法:
//
private void ForwardSelectedMessage()
{
try
{
// create a new mail message
Outlook.MailItem ForwardingMessage =
(Outlook.MailItem)this.Application.CreateItem(
Outlook.OlItemType.olMailItem);
// In case you want to implement the custom outlook
// form you have use following commented 2 lines of code
//Outlook.MAPIFolder templateFolder =
// this.Application.Session.GetDefaultFolder(
// Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
//ForwardingMessage = (Outlook.MailItem)templateFolder.Items.Add(
// "IPM.Note.OutlookCustomForm1");
// get the currently selected message
// (the message on which the user right-clicked)
Outlook.MailItem SelectedMessage;
SelectedMessage = (Outlook.MailItem)
this.Application.ActiveExplorer().Selection[1];
if (SelectedMessage != null)
{
if (ForwardingMessage != null)
{
//Subject
ForwardingMessage.Subject = "FW: " + SelectedMessage.Subject;
#region Attahment
// Get the count of Attachments
int attachCount = SelectedMessage.Attachments.Count;
if (attachCount > 0)
{
// loop through each attachment
for (int idx = 1; idx <= attachCount; idx++)
{
string sysPath = System.IO.Path.GetTempPath();
if (!System.IO.Directory.Exists(sysPath + "~test"))
{
System.IO.Directory.CreateDirectory(sysPath + "~test");
}
// get attached file and save in temp folder
string strSourceFileName = sysPath + "~test\\" +
SelectedMessage.Attachments[idx].FileName;
SelectedMessage.Attachments[idx].SaveAsFile(strSourceFileName);
string strDisplayName = "Attachment";
int intPosition = 1;
int intAttachType = (int)Outlook.OlAttachmentType.olEmbeddeditem;
// Add the current attachment
ForwardingMessage.Attachments.Add(strSourceFileName,
intAttachType, intPosition, strDisplayName);
}
}
#endregion
#region Body
string strHeader = "<p><br><br>" +
"-----Original Message-----" + "<br>";
strHeader += "From: " + SelectedMessage.SenderName + "<br>";
strHeader += "Sent: " + SelectedMessage.SentOn.ToString() + "<br>";
strHeader += "To: " + SelectedMessage.To + "<br>";
strHeader += "Subject: " + SelectedMessage.Subject + "<br><br>";
ForwardingMessage.HTMLBody = strHeader +
GetTrimmedBodyText(SelectedMessage.HTMLBody);
#endregion
ForwardingMessage.Display(false);
}
}
}
catch (Exception ex)
{
if (ex.Message.ToLower().StartsWith("unable to cast com object"))
{
MessageBox.Show("This is not a valid message and it can not be sent.",
"", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("ForwardSelectedMessage - " + ex.Message,
"", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#region Handling inline images in the message body
/* by defaule the inline images in the selected message
will be added as attachments to the new message
the reason is in htmlbody the image src will be as follows:
src = "cid:image001.jpg@01C82869373A65B0"
to view inline images (and avoid image attachments) it should changed as follows:
src = "image001.jpg"
replace "cid" with "" (empty string) and
replace "image001.jpg@01C82869373A65B0" with "image001.jpg" (only file name)
*/
private string GetTrimmedBodyText(string strHTMLBody)
{
string strTrimmedHTMLBody = strHTMLBody;
int start = 0;
start = strHTMLBody.IndexOf("<img", start);
/* the following loop will check for each src attribute value for each image and
it will replace them only with image file name (by removing cid: and @ part)
*/
while (start > 0)
{
int count = strHTMLBody.IndexOf('>', start);
string str = strHTMLBody.Substring(start);
string strActualImgTag = str.Substring(0, str.IndexOf(">") + 1);
string strTrimImgTag = strActualImgTag.Replace("cid:", "");
int intAtPosition = 0;
intAtPosition = strTrimImgTag.IndexOf("@");
while (intAtPosition > 0)
{
string strAt =
strTrimImgTag.Substring(strTrimImgTag.IndexOf("@"), 18);
strTrimImgTag = strTrimImgTag.Replace(strAt, "");
intAtPosition = strTrimImgTag.IndexOf("@");
}
strTrimmedHTMLBody =
strTrimmedHTMLBody.Replace(strActualImgTag, strTrimImgTag);
start = strHTMLBody.IndexOf("<img", start + 1);
}
return strTrimmedHTMLBody;
}
#endregion
#endregion
//