是否可以像Outlook RTF/TNEF一样,在HTML电子邮件中嵌入非图像附件



当您使用Outlook发送电子邮件时,您可以选择RTF格式而不是HTML,从而允许您在电子邮件主体中嵌入诸如PDFS内联的附件。当您连接多个文件并想在体内引用它们时,这可能非常方便。如果您在Outlook中选择HTML格式,这是不再可能的,并且附带的图像出现在电子邮件正文之外(例如"正常")。

我正在以编程方式发送电子邮件,并想知道是否可以以某种方式将附件嵌入附件。您可以使用图像来执行此操作,使用诸如<img src="cid:image_id@somethingorother" />之类的标签,涉及Multipart电子邮件中的嵌入式图像。有没有办法使用其他类型的附件来执行此操作?

我主要对我将电子邮件发送给所有在同一MS Exchange电子邮件服务器上的收件人,使用Outlook作为邮件客户端的收件人感兴趣。有时,他们会使用其他电子邮件客户端或外部发送,如果在这种情况下电子邮件降低了"正常"附件是可以的。

我可以使用RTF/TNEF格式,但是:

  • 这似乎很难,
  • 我的电子邮件的内容在HTML中,所以我首先必须将RTF转换为HTML。

我决定不那么懒惰,然后自己尝试...

答案是,您可以使用<a href='cid:...'> . </a>标签在HTML电子邮件中参考非图像附件,Outlook将打开它们。我将Outlook 2013用作电子邮件客户端和Office365作为服务器;里程与不同的客户和服务器不同。但是,如果您不使用Outlook ...

,效果不佳

gmail

我也测试了发送到Gmail的。内联图像正常工作,但附件却没有。显示<a>链接但不起作用,如果您在电子邮件正文中使用cid:... URL参考附件,即使是dendosition:artivement :( Gmail iPhone应用程序上的相同行为也不会显示它图像看起来不错,内联附件不会从<a>链接显示或打开。

iPhone邮件

iPhone的邮件应用程序(连接到Office 365)将<a>标签作为链接呈现,但它们不起作用。如果将附件处置设置为"附件"(即非"内联"),则将附件显示在电子邮件底部,这与Gmail不同,在这种情况下会隐藏它们。如果将附件处置设置为"内联",则会隐藏附件。

因此,如果您有Gmail收件人/电子邮件客户端,您需要做一些不同的事情,甚至可能将文件附加两次:一次作为性格:内联(链接到身体内部),并且一次为dectosition:附件。可悲的是,gmail不显示具有倾向的附件:附件并提及使用CID,因为这至少意味着有一种访问它们的方法。如果有人有任何建议,请告诉我!

示例代码

这是我使用EWS用于测试的PowerShell。它发送了带有

的HTML电子邮件
  • 一个嵌入式图像(从不出现为"附件")
  • a .pdf与<a>标签链接,标记为内联。在Outlook中,这不是作为附件出现的,但可以从链接访问。在Gmail和iOS邮件中,这都不显示为附件,也无法从链接访问。
  • a .docx文件与<a>标签链接并标记为附件。在Outlook中,这是一个附件,也可以从链接访问。在gmail中,这没有显示,也无法从链接中使用。在iOS邮件中,这显示为附件,但无法从链接访问。
  • 再次标记为附件,与<a>标签链接相同。这在Outlook,Gmail,iOS邮件中可见。

PowerShell:

$to1 = 'your-email@gmail.com'
$to2 = 'your-email@your-domaincom'
$subject = 'Test email with embedded attachments'
$body = '
This email shows embedded attachments. Yay. <br/><br/>
Here is an image: <img src="cid:img1@your-domain.com" /><br/>
More amazingly, <a href="cid:att1@your-domain.com">here</a> is a pdf.<br/>
And <a href="cid:att2@your-domain.com">here</a> is a word doc!<br/><br/>
'
# fyi - the '@your-domain.com' in the id is optional but somewhere I read 
# that email clients might like it more if it's included. Doesn't need to 
# be a real domain.
# change these paths to something that exists
$image1Path = "C:tempan_image.jpg"
$attachment1Path = "C:tempsome_file.pdf"
$attachment2Path = "C:tempsome_doc.docx"
#prompt for Office365 creds
$Credential = Get-Credential
#Load the EWS Managed API Assembly - you need it installed first!!
Add-Type -Path 'C:Program Files (x86)MicrosoftExchangeWeb Services2.1Microsoft.Exchange.WebServices.dll'
#Instantiate the EWS service object                  
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
#Set the credentials for Exchange Online
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList  $Credential.UserName, $Credential.GetNetworkCredential().Password
#Autodiscover doesn't seem to work for my office365; set it manually
$service.Url = 'https://outlook.office365.com/EWS/Exchange.asmx'
#This might work for you instead: 
# $service.AutodiscoverUrl($Credential.UserName, {$true})
#Create the email message and set the Subject and Body
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
$message.Subject = $subject
$message.Body = $body
$null = $message.ToRecipients.Add($to1)
$null = $message.ToRecipients.Add($to2)
$att = $message.Attachments.AddFileAttachment($image1Path)
$att.ContentId = 'img1@your-domain.com'
$att.IsInline=$true
$att = $message.Attachments.AddFileAttachment($attachment1Path)
$att.ContentId = 'att1@your-domain.com'
$att.IsInline=$true
$att = $message.Attachments.AddFileAttachment($attachment2Path)
$att.ContentId = 'att2@your-domain.com'
$att.IsInline=$false
# add the same attachment again with a different name to see if it's 
# rendered differently.
$att = $message.Attachments.AddFileAttachment('no_cid.docx', $attachment2Path)
$att.IsInline=$false
#Send the message and save a copy in the Sent Items folder
$message.SendAndSaveCopy()

和一些方便的文章:

  • 使用PowerShell在线发送Exchange的电子邮件
  • 使用EWS托管API 2.0
  • 添加内联附件

最新更新