【问题标题】:Perl - Outlook - Including an embedded image into body of email (HTML)Perl - Outlook - 将嵌入图像包含在电子邮件正文中(HTML)
【发布时间】:2020-02-08 18:26:48
【问题描述】:

我需要在电子邮件正文中包含一个图像(使用 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;

有人知道上面的代码有什么问题吗?任何建议都非常感谢。

【问题讨论】:

    标签: image perl outlook com-automation


    【解决方案1】:

    您需要附加一张图片,然后设置一个可以在消息正文中使用的 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 );
    

    【讨论】:

    • 感谢缺少 CID(内容 ID)和 SetProperty 方法的提示。您是否也有相同代码的 perl 版本而不是 vbs?
    • 我对 Perl 一无所知,这是 C#。但是 Outlook 对象模型为所有编程语言提供了相同的方法。所以,代码是一样的。
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 2022-09-25
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2011-10-06
    • 2015-03-14
    相关资源
    最近更新 更多