首页 > 解决方案 > Perl - Outlook - 将嵌入图像包含在电子邮件正文中(HTML)

问题描述

我需要将图像包含到电子邮件正文中(使用 HTML 格式),以便将其嵌入,而不是使用 Perl 中的库 Win32::OLE 调用安装在 Windows 上的 Outlook 客户端的额外附件。

下面的代码有效,但收到的电子邮件不显示图像。我可以看到唯一的一个小方形符号,表示内嵌嵌入图像丢失或损坏。

#use strict;
use warnings;
use Win32::OLE;

#get new Outlook instance
my $mail = new Win32::OLE('Outlook.Application');
die "Unable to start Outlook instance: $!" if !defined $mail;

my $item = $mail->CreateItem(0);
die "Unable to create mail item: $!" if !defined $item;

$item->{'To'} = 'mypersonalgmailaddress@gmail.com'; 
$item->{'CC'} = '';
$item->{'Subject'} = 'Test for embedded/inline image';
$item->{'BodyFormat'} = 'olFormatHTML'; 
$item->{'HTMLBody'} = "<html><body><img src=\"signature.png\"></body></html>";
$item->save();

#attach an image and make it embedded.
my $attachments = $item->Attachments();
$attachments->Add('C:\signature.png', olEmbeddeditem);

#send it
$item->Send();

my $error = Win32::OLE->LastError();
print STDERR "Win32::OLE error: $error" if $error;

有人知道上面的代码有什么问题吗?任何建议都将受到高度赞赏。

标签: imageperloutlookcom-automation

解决方案


您需要附加图像,然后设置可以在消息正文中使用的 CID:

Attachment attachment = newMail.Attachments.Add(@"E:\Pictures\image001.jpg"
    , OlAttachmentType.olEmbeddeditem , null , "Some image display name" );

   string imageCid = "image001.jpg@123";

   attachment.PropertyAccessor.SetProperty(
     "http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);

   newMail.HTMLBody = String.Format("<body><img src=\"cid:{0}\"></body>", imageCid );

推荐阅读