我决定不那么懒惰,自己尝试一下……
答案是是的,您可以使用<a href='cid:...'> . </a> 标记引用html 电子邮件中的非图像附件,Outlook 将打开它们。我使用 Outlook 2013 作为我的电子邮件客户端和 Office365 作为我的服务器;里程因不同的客户端和服务器而异。 但是如果你不使用outlook,效果就不那么好了...
Gmail
我也测试了发送到 gmail。内联图像可以正常工作,但附件不能。 <a> 链接已显示但不起作用,如果您在电子邮件正文中使用 cid:... url 引用附件,即使使用 disposition:attachment gmail 也不会显示它:( gmail 上的相同行为iPhone 应用:内嵌图像看起来不错,内嵌附件不显示或从 <a> 链接打开。
iPhone 邮件
iPhone 的邮件应用程序(连接到 Office 365)将 <a> 标记呈现为链接,但它们不起作用。如果您将附件处置设置为“附件”(即不是“内联”),则附件将显示在电子邮件的底部,这与在这种情况下隐藏它们的 gmail 不同。如果您将附件处置设置为“内联”,则它会隐藏附件。
因此,如果您有 gmail 收件人/电子邮件客户端,您需要做一些不同的事情,甚至可能附加两次文件:一次作为 disposition:inline(链接到正文内),一次作为 disposition:attachment。遗憾的是,Gmail 不显示具有 disposition:attachment 并使用 cid 引用的附件,因为这至少意味着有一种访问它们的方法。如果有人有任何建议,请告诉我!
示例代码
这是我使用 EWS 进行测试的 powershell。它发送带有
的 html 电子邮件
- 嵌入图像(从不显示为“附件”)
- 链接到带有
<a> 标记的.pdf,标记为内联。在 Outlook 中,这不会显示为附件,但可以通过链接访问。在 gmail 和 iOS 邮件中,这不会显示为附件,也无法通过链接访问。
- .docx 文件与
<a> 标记链接并标记为附件。在 Outlook 中,这显示为附件,也可以从链接访问。在 gmail 中,这不会显示,也不能通过链接工作。在 iOS 邮件中,这显示为附件,但无法通过链接访问。
- 同样的 .docx 文件再次标记为附件,并且未与
<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:\temp\an_image.jpg"
$attachment1Path = "C:\temp\some_file.pdf"
$attachment2Path = "C:\temp\some_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)\Microsoft\Exchange\Web Services\2.1\Microsoft.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()
还有一些方便的文章: