【问题标题】:Is it possible to embed non-image attachments inline in an html email, like Outlook RTF/TNEF does?是否可以像 Outlook RTF/TNEF 那样在 html 电子邮件中嵌入非图像附件?
【发布时间】:2015-07-19 23:10:41
【问题描述】:

当您使用 Outlook 发送电子邮件时,您可以选择 RTF 格式而不是 HTML,从而允许您在电子邮件正文中嵌入 PDF 等附件。当您附加多个文件并希望在正文中引用它们时,这会非常方便。如果您在 Outlook 中选择 HTML 格式,这将不再可能,并且附加图像会出现在电子邮件正文之外(如“正常”)。

我正在以编程方式发送电子邮件,并想知道是否可以以某种方式将附件嵌入到电子邮件正文中。 You can do this with images,使用<img src="cid:image_id@somethingorother" /> 之类的标签,指代多部分电子邮件中的嵌入图像。有没有办法用其他类型的附件做到这一点?

我主要对使用 Outlook 作为邮件客户端向位于同一 MS Exchange 电子邮件服务器上的收件人发送电子邮件的场景感兴趣。有时他们会使用不同的电子邮件客户端,或从外部发送,如果电子邮件在这种情况下降级为“正常”附件,那也没关系。

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

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

【问题讨论】:

    标签: .net email exchange-server html-email email-attachments


    【解决方案1】:

    我决定不那么懒惰,自己尝试一下……

    答案是是的,您可以使用<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()
    

    还有一些方便的文章:

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 2010-11-11
      • 2011-04-16
      • 2012-03-30
      • 2011-10-06
      • 2015-03-14
      • 2022-09-25
      • 1970-01-01
      相关资源
      最近更新 更多