Ror*_*ory 3 .net email exchange-server html-email email-attachments
使用Outlook发送电子邮件时,您可以选择RTF格式而不是HTML,从而可以将诸如PDF之类的附件内嵌在电子邮件正文中。当您要附加多个文件并希望在正文中引用它们时,这可能非常方便。如果您在Outlook中选择HTML格式,则将不再可能,并且附件图像会显示在电子邮件正文之外(例如“正常”)。
我正在以编程方式发送电子邮件,想知道是否有可能以某种方式将附件内嵌在电子邮件正文中。您可以使用images来实现此目的,可以使用标记,例如<img src="cid:image_id@somethingorother" />引用多部分电子邮件中的嵌入式图像。是否可以使用其他类型的附件执行此操作?
我主要对以下情况感兴趣:我将使用Outlook作为邮件客户端的电子邮件发送给都在同一MS Exchange电子邮件服务器上的收件人。有时他们会使用其他电子邮件客户端,或者通过外部发送,在这种情况下电子邮件降级为“正常”附件也可以。
我可以选择使用RTF / TNEF格式,但是:
我决定减少懒惰,自己尝试一下...
答案是肯定的,您可以使用<a href='cid:...'> . </a>标签引用html电子邮件中的非图像附件,Outlook会打开它们。我将Outlook 2013用作电子邮件客户端,将Office365用作服务器。里程因客户和服务器的不同而异。但是如果您不使用Outlook,效果就不会很好...
邮箱
我也测试过发送到gmail。内联图像可以正常工作,但附件则不能。该<a>链接显示,但不工作,如果你是指用附件cid:...的电子邮件Gmail的体内网址甚至不与配置其显示:附件:(上的Gmail的iPhone应用程序相同的行为:内嵌图片看好的,内联附件不会显示或无法通过<a>链接打开。
iPhone邮件
iPhone的Mail应用程序(连接到Office 365)将<a>标签呈现为链接,但它们不起作用。如果将附件处置设置为“附件”(即不是“内联”),则附件将显示在电子邮件的底部,这与gmail(在这种情况下将其隐藏)不同。如果将附件处置设置为“内联”,则它将隐藏附件。
因此,如果您有Gmail收件人/电子邮件客户端,则需要做一些不同的事情,甚至可能还要附加两次文件:一次为disposition:inline(链接到正文中),一次为disposition:attachment。令人遗憾的是,Gmail没有显示具有disposition:attachment且使用cid引用的附件,因为这至少意味着有一种访问它们的方法。如果有人有任何建议,请告诉我!
样例代码
这是我用于使用EWS进行测试的功能。它发送带有
<a>标记链接的.pdf 标记为内联。在Outlook中,它不会显示为附件,但可以通过链接进行访问。在gmail和iOS Mail中,它都不会显示为附件,因此无法通过链接进行访问。<a>标签链接并标记为附件的.docx文件。在Outlook中,它显示为附件,也可以从链接进行访问。在gmail中,这不会显示,因此无法通过链接运行。在iOS Mail中,它显示为附件,但无法从链接访问。<a>标记链接。这在Outlook,gmail,iOS Mail中作为附件可见。电源外壳:
$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()
和一些方便的文章:
| 归档时间: | 
 | 
| 查看次数: | 1384 次 | 
| 最近记录: |