Nea*_*ers 6 c# mime biztalk email-attachments biztalk-2016
我正在使用BizTalk 2016 SMTP发送端口创建一个带有MIME附件的电子邮件.但是,我认为任何人都可以从任何其他语言中分享有关Outlook和MIME的奇怪之处的任何知识可能会帮助我解决下面的问题.
在Outlook中,附件显示为body.txt,但是当我单击"文件保存"时,它会显示我在创建它时使用的名称(这是用户想要查看的名称).
我所指的是左侧,在5k上方的"body.txt"和下面屏幕截图中附件图标的右侧:
在BizTalk C#Pipeline组件中,使用以下代码设置该附件,我们在BizTalk消息上设置Context属性.我也尝试过设置ContentHeader和ContentID.
strFilename = "MyFileName_693.txt"; // Just for example.
pInMsg.BodyPart.PartProperties.Write(
"FileName",
"http://schemas.microsoft.com/BizTalk/2003/mime-properties",
strFilename);
Run Code Online (Sandbox Code Playgroud)
当我将电子邮件转发到我的Gmail时,附件显示了正确的名称.所以我的问题特别是让它在Outlook(2016)中显示所需的名称.
到目前为止,我已经将其与具有动态发送端口的编排一起使用。这仍然是一项工作,但它可以使用库存组件完成工作。以下描述基于 BizTalk 2013R2 中包含的库存 SMTP 适配器。
注意:即使我的解决方案有效,但如果适配器对此稍微聪明一点,它感觉就像是一种解决方法,并且是我不应该做的事情。
首先,让我们看一下在某些客户端中导致问题的示例电子邮件片段:
------=_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)...
Run Code Online (Sandbox Code Playgroud)
注意这个Content-Description: body
部分。body.xml
这就是为什么有些客户会阅读或 在我的例子中的原因body.pdf
,尽管处置部分看起来很棒:Content-Disposition: attachment; filename="CDM_Order - Copy.pdf"
。
硬设置MIME.FileName
不仅会起作用,即使它 Content-Disposition
最终会设置正确,但它永远不会更新Content-Description
. 这是因为您在静态发送端口上设置了Attach only body part
或指定了相应的数值1
或在动态发送端口上
但是,它将与type 的Attach all parts
或值一起使用。这涉及在您的编排中制作多部分消息。这将有两个部分;2
MessagePartsAttachments
BodyPart
,现在这个将包含您的消息文本,而不是您的附件。确保您Message Body Part
按照 中的方式指定了此项Message Type
。Attachment
在这个例子中命名了它。现在您可能认为它也会发送BodyPart
附件,因为我已经说过我们需要Attach all parts
。这是正确的,因此要纠正这个问题,您BodyPart
必须定义为 a RawString
,这会将字符串转换为 BizTalk 消息部分中的纯文本。为了完整起见,我将 C# 类放在底部以供参考。
现在它被定义为 a RawString
,SMTP 适配器将把它作为正文而不是作为附件。副作用是,SMTP 适配器将不再将该Content-Description: body
部分放在附件部分中,而是放在实际的正文部分中。它看起来像这样:
------=_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)...
Run Code Online (Sandbox Code Playgroud)
实际上,除了零件的放置之外,没有其他任何不同Content-Description: body
,这正是我们想要的。现在,该电子邮件对于每个客户来说看起来都很好。
除了我已经提到的属性之外,还必须设置最重要的属性以使其正常运行:
您的正文的内容类型:
MsgPdfOrder.BodyPart(Microsoft.XLANGs.BaseTypes.ContentType) = "text/plain";
Run Code Online (Sandbox Code Playgroud)
您附件的内容类型:
MsgPdfOrder.Attachment(Microsoft.XLANGs.BaseTypes.ContentType) = "application/pdf";
Run Code Online (Sandbox Code Playgroud)
附件文件名:
MsgPdfOrder.Attachment(MIME.FileName) = "CDM_Order - Copy.pdf"
Run Code Online (Sandbox Code Playgroud)
Unknown Error Description
正文字符集(如果不设置将会导致):
MsgPdfOrder(SMTP.EmailBodyTextCharset) = "UTF-8";
Run Code Online (Sandbox Code Playgroud)
确保您没有设置 ,SMTP.EmailBodyText
因为我们已经BodyPart
为此设置了 。
RawString 类,在编排中像这样使用它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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
963 次 |
最近记录: |