如何设置附件名称以在Outlook中正确显示 [英] How Set Attachment Name to Show Properly in Outlook

查看:433
本文介绍了如何设置附件名称以在Outlook中正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从BizTalk 2016 SMTP发送端口创建带有MIME附件的电子邮件. 但是,我认为任何人都可以从任何其他语言共享的有关Outlook和MIME奇怪之处的任何知识都可以帮助我解决以下问题.

I'm creating an email with a MIME attachment from a BizTalk 2016 SMTP Send Port. However, I think any knowledge that anyone can share from any other language about the oddities of Outlook and MIME might help me fix the issue below.

在Outlook中,附件显示为body.txt,但是当我单击文件保存"时,它会显示我创建它时使用的名称(这就是用户想要看到的名称).

In Outlook, the attachment shows as body.txt, but when I click "File Save" it shows the name that I used when I created it (and that's what the user wants to see).

我指的是在5k上方左侧的左侧显示"body.txt",并在以下屏幕截图中的附件图标右侧:

What I'm referring to is the the left side where it says "body.txt" above the 5k and to the right of the attachment icon in the screen shot below:

在BizTalk C#管道组件中,该附件是使用以下代码设置的,其中我们在BizTalk消息上设置了上下文属性. 我也尝试设置ContentHeader和ContentID.

In BizTalk C# Pipeline component, that attachment was set with the following code, where we are setting Context properties on the BizTalk Message. I also tried setting ContentHeader and ContentID.

strFilename = "MyFileName_693.txt";  // Just for example. 
pInMsg.BodyPart.PartProperties.Write(
              "FileName",
              "http://schemas.microsoft.com/BizTalk/2003/mime-properties",
               strFilename);

当我将电子邮件转发到我的Gmail时,附件将显示为正确的名称.因此,我的问题特别是要使其在Outlook(2016)中以所需的名称显示.

When I forwarded the email to my Gmail, the attachment was shown with the proper name. So my question is particular to making it appear with the desired name in Outlook (2016).

推荐答案

到目前为止,我已经使用带有动态发送端口的业务流程来进行此工作.仍然有些工作,但是可以通过库存组件完成工作.以下描述基于BizTalk 2013R2中包含的常规SMTP适配器.

So far I've got this working with an orchestration with a dynamic send port. It's still a bit of work, but it gets the job done with the stock component. Following description is based on the stock SMTP-adapter included in BizTalk 2013R2.

注意:即使我的解决方案可以工作,但如果适配器对此稍微更聪明,那感觉就像是一种变通方法,而我不需要做的事情.

首先,让我们看一下导致某些客户端出现问题的示例电子邮件片段:

First of all, let's look example email snippet which causes issues in some clients:

------=_NextPart_000_0001_01D4502F.8A6A1500
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="utf-8"

See attached email.
------=_NextPart_000_0001_01D4502F.8A6A1500
Content-Type: application/pdf; name="CDM_Order - Copy.pdf"
Content-Disposition: attachment; filename="CDM_Order - Copy.pdf"
Content-Description: body
Content-Transfer-Encoding: base64

JVBERi0xLjQKJeLjz9MNCjUgMCBvYmoKPDwvRFsgMyAwIFIvWFlaIG51bGwgODQxLjg4OTc3IG51
bGwgXQo+PgplbmRvYmoKOCAwIG9iago8PC9EWyAzIDAgUi9YWVogbnVsbCAyOTAuMjM2NTcgbnVs
bCBdCj4+ (etc etc base64 your file)...

注意Content-Description: body部分.这就是为什么某些客户端读取body.xml或在我的情况下为body.pdf的原因,即使处置"部分看起来很棒:Content-Disposition: attachment; filename="CDM_Order - Copy.pdf".

Notice the Content-Description: body part. This is the reason why some clients read body.xml or in my case body.pdf, even though the Disposition part looks great: Content-Disposition: attachment; filename="CDM_Order - Copy.pdf".

硬设置MIME.FileName不仅会起作用,即使最终会正确设置Content-Disposition,也永远不会更新Content-Description.这是因为在静态发送端口上已设置Attach only body part或在动态发送端口上指定了相应的数值1.

Hard setting MIME.FileName isn't just going to work, even though it will set the Content-Disposition right eventually, it'll never update the Content-Description. This is because either on a static send port you've set the Attach only body part or you specified the corresponding numeric value 1 on a dynamic send port.

但是,它将与类型MessagePartsAttachmentsAttach all parts2值一起使用.这涉及在业务流程中制作多部分消息.这将分为两个部分:

However, it will work with the Attach all parts or 2 value for the type MessagePartsAttachments. This involves making a multi-part message in your orchestration. This will have two parts;

  • 第一个是BodyPart,现在这将包括您的消息文本而不是附件.确保在Message Type中将此名称指定为Message Body Part.
  • 第二部分将是您的实际附件,请根据您的附件类型指定此类型.在此示例中,我将此名称命名为Attachment.
  • First one is the BodyPart, now this one will include your message text and not your attachment. Make sure you specify this one as Message Body Part in the Message Type.
  • Second part will be your actual attachment, specify this type according to your attachment type. I named this Attachment in this example.

现在,您可能会认为它也会发送BodyPart作为附件,因为我已经说过我们需要Attach all parts.这是正确的,因此要纠正这一点,必须将您的BodyPart定义为RawString,这会将字符串在BizTalk消息部分中转换为纯文本.为了完整起见,我将C#类放在底部以供参考.

Now you might think it will send the BodyPart as attachment as well since i've said we needed Attach all parts. This is true, so to correct that, your BodyPart has to be defined as a RawString, this turns the string into plain text in the BizTalk message part. For completeness i'll put the C# class at the bottom for reference.

现在,将其定义为RawString,SMTP适配器会将其作为正文而不是作为附件.副作用是,SMTP适配器不再将Content-Description: body部分放在附件部分中,而是放在实际的主体部分中.看起来像这样:

Now that it's defined as a RawString, the SMTP adapter will put this as the body instead of as attachment. As a side effect, the SMTP adapter will no longer put the Content-Description: body part in the attachment part, but in the actual body part instead. It looks like this:

------=_NextPart_000_0001_01D450E4.A7E9A5E0
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="utf-8"
Content-Description: body

See attached email.
------=_NextPart_000_0001_01D450E4.A7E9A5E0
Content-Type: application/pdf; name="ID_0_Nummer_0.pdf"
Content-Disposition: attachment; filename="ID_0_Nummer_0.pdf"
Content-Transfer-Encoding: base64

JVBERi0xLjQKJeLjz9MNCjUgMCBvYmoKPDwvRFsgMyAwIFIvWFlaIG51bGwgODQxLjg4OTc3IG51
bGwgXQo+PgplbmRvYmoKOCAwIG9iago8PC9EWyAzIDAgUi9YWVogbnVsbCAyOTAuMjM2NTcgbnVs
bCBdCj4+ (etc etc base64 your file)...

实际上,除了Content-Description: body部分的位置以外,没有什么不同,正是我们想要的.现在,每个客户的电子邮件看起来都很好.

Really nothing else is different except the placement of the Content-Description: body part, exactly what we want. Now the email looks fine for every client.

除了我已经提到的那些属性外,最重要的属性也必须设置为使其正常运行:

The most important properties, besides the ones i already mentioned, must be set as well to make it behave properly:

您身体的内容类型:

MsgPdfOrder.BodyPart(Microsoft.XLANGs.BaseTypes.ContentType) = "text/plain";

附件的内容类型:

MsgPdfOrder.Attachment(Microsoft.XLANGs.BaseTypes.ContentType) = "application/pdf";

附件文件名:

MsgPdfOrder.Attachment(MIME.FileName) =  "CDM_Order - Copy.pdf"

正文字符集(如果未设置,则结果为Unknown Error Description)

Body character set (will result in Unknown Error Description if not set):

MsgPdfOrder(SMTP.EmailBodyTextCharset) = "UTF-8";

请确保您未设置SMTP.EmailBodyText,因为我们已经为此设置了BodyPart.

Make sure you don't set the SMTP.EmailBodyText because we already have the BodyPart for that.

RawString类,在业务流程MsgPdfOrder.BodyPart = new Yournamespace.Components.RawString("See attached email.");中像这样使用它:

RawString class, use it like this in an orchestration MsgPdfOrder.BodyPart = new Yournamespace.Components.RawString("See attached email."); :

using System.Runtime.Serialization;
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Microsoft.XLANGs.BaseTypes;

namespace Yournamespace.Components
{
    public abstract class BaseFormatter : IFormatter
    {
        public virtual SerializationBinder Binder
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        public virtual StreamingContext Context
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        public virtual ISurrogateSelector SurrogateSelector
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        public abstract void Serialize(Stream stm, object obj);
        public abstract object Deserialize(Stream stm);
    }

    public class RawStringFormatter : BaseFormatter
    {
        public override void Serialize(Stream s, object o)
        {
            RawString rs = (RawString)o;
            byte[] ba = rs.ToByteArray();
            s.Write(ba, 0, ba.Length);
        }

        public override object Deserialize(Stream stm)
        {
            StreamReader sr = new StreamReader(stm, true);
            string s = sr.ReadToEnd();
            return new RawString(s);
        }
    }

    [CustomFormatter(typeof(RawStringFormatter))]
    [Serializable]
    public class RawString
    {
        [XmlIgnore]
        string _val;

        public RawString(string s)
        {
            if (null == s)
                throw new ArgumentNullException();
            _val = s;
        }

        public RawString()
        {
        }

        public byte[] ToByteArray()
        {
            return Encoding.UTF8.GetBytes(_val);
        }

        public override string ToString()
        {
            return _val;
        }
    }
}

这篇关于如何设置附件名称以在Outlook中正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆