.net – 使用Exchange Web服务发送包含特殊字符的电子邮件

.net – 使用Exchange Web服务发送包含特殊字符的电子邮件,第1张

概述尝试使用 Exchange Web Services Managed API针对Exchange Server 2010 SP1发送电子邮件时,如果收件人电子邮件地址(@的左侧)包含丹麦语字母æøå,则会引发ServiceException,其中包含ErrorCode ErrorInvalidRecipients. 这些字符是不允许在电子邮件地址的本地部分,还是我应该以某种方式编码地址? 更新:这 尝试使用 Exchange Web Services Managed API针对Exchange Server 2010 SP1发送电子邮件时,如果收件人电子邮件地址(@的左侧)包含丹麦语字母æøå,则会引发ServiceException,其中包含ErrorCode ErrorInvalidRecipients.

这些字符是不允许在电子邮件地址的本地部分,还是我应该以某种方式编码地址?

更新:这是测试源代码和跟踪数据:

var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);service.autodiscoverUrl("my@email.com");EmailMessage email = new EmailMessage(service);email.Body = "Test";email.Subject = "Test";email.ToRecipIEnts.Add("æøå@domain.com");email.Send();

来自EWS的跟踪数据显示以下内容:

请求标头:

POST /EWS/Exchange.asmx http/1.1Content-Type: text/xml; charset=utf-8Accept: text/xmlUser-Agent: ExchangeServicesClIEnt/14.03.0032.000Accept-EnCoding: gzip,deflate

请求:

<?xml version="1.0" enCoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:header>    <t:RequestServerVersion Version="Exchange2010_SP1" />  </soap:header>  <soap:Body>    <m:CreateItem Messagedisposition="SendOnly">      <m:Items>        <t:Message>          <t:Subject>Test</t:Subject>          <t:Body BodyType="HTML">Test</t:Body>          <t:ToRecipIEnts>            <t:MailBox>              <t:EmailAddress>æøå@domain.com</t:EmailAddress>            </t:MailBox>          </t:ToRecipIEnts>        </t:Message>      </m:Items>    </m:CreateItem>  </soap:Body></soap:Envelope>

响应:

<?xml version="1.0" enCoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:header>    <h:ServerVersionInfo MajorVersion="14" MinorVersion="1" MajorBuildNumber="438" MinorBuildNumber="0" Version="Exchange2010_SP1" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />  </s:header>  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    <m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">      <m:ResponseMessages>        <m:CreateItemResponseMessage ResponseClass="Error">          <m:MessageText>One or more recipIEnts are invalID.</m:MessageText>          <m:ResponseCode>ErrorInvalIDRecipIEnts</m:ResponseCode>          <m:DescriptivelinkKey>0</m:DescriptivelinkKey>          <m:Items />        </m:CreateItemResponseMessage>      </m:ResponseMessages>    </m:CreateItemResponse>  </s:Body></s:Envelope>

最后,在Outlook中创建一个这样的地址的电子邮件给出了这样的信息:

解决方法 根据本文处理Exchange收件人解析,您可能需要使用此方法对这些字符进行编码:

Alphanumeric characters,the equal sign (=) and the hyphen (-) don’t
require enCoding. Other characters use the following enCoding Syntax:
A forward slash (/) is replaced by an underscore (_). Other US-ASCII
characters are replaced by a plus sign (+) and the two digits of its
ASCII value are written in hexadecimal. For example,the space
character has the encoded value +20.

http://technet.microsoft.com/en-us/library/bb430743(v=exchg.141).aspx

所以你的“æøå@domain.com”的例子将成为“e6 f8 e5@domain.com”

编辑:

如果地址的“域”部分使用特殊字符,则还需要使用不同的方法对其进行编码.

这是一个GetExchangeEncodedRecipIEnt助手方法,我把它放在一起使用RFC 5321和RFC 3461规范对电子邮件地址进行编码.

public static string GetExchangeEncodedRecipIEnt(string recipIEnt) {        int atIDx = recipIEnt.LastIndexOf('@');        if (atIDx < 0) {            throw new ArgumentException("Unable to parse domain portion of \"recipIEnt\" email address.");        }        string namepart = recipIEnt.Substring(0,atIDx);        string domainPart = recipIEnt.Substring(atIDx + 1);        //need to encode any special characters in the domain name        System.Globalization.IDnMapPing punycode = new System.Globalization.IDnMapPing();        domainPart = "@" + punycode.GetAscii(domainPart);        return String.Concat(namepart.Select(c => GetExchangeEncodedChar(c))) + domainPart;    }    private static string GetExchangeEncodedChar(char c) {        string encodedChar = c.ToString();        int charaSCIICode = (int)c;        //Encode according to RFC5321,https://tools.IEtf.org/HTML/rfc5321#section-4.1.2        //  which references rfc3461 "xtext" format. https://tools.IEtf.org/HTML/rfc3461#section-4         if(charaSCIICode >= 33 && charaSCIICode <= 126 && c != '+' && c != '=')        {            //This is a character in the valID 33-126 ASCII range for email addresses,which does not need encoded.            return c.ToString();        }        else if(c == '/'){            //A forward slash (/) is replaced by an underscore (_).            return "_";        }        else {             return "+" + ((int)c).ToString("x2").toupper();        }    }

然后,您可以使用GetExchangeEncodedRecipIEnt对地址进行编码.这是一个例子:

string recipIEnt = @"user1æøå@dømain.com";string encodedAddress = GetExchangeEncodedRecipIEnt(recipIEnt);Console.Writeline("Original: {0},Encoded: {1}",recipIEnt,encodedAddress);

对于上面的示例收件人,将输出:

Original: user1æøå@dømain.com,Encoded: user1+E6+F8+E5@xn--dmain-vua.com

如果您使用的“普通”电子邮件地址不包含“普通”ASCII范围之外的任何字符,那么它将返回相同的字符串.

Original: Jsmith01@gmail.com,Encoded: Jsmith01@gmail.com

总结

以上是内存溢出为你收集整理的.net – 使用Exchange Web服务发送包含特殊字符的电子邮件全部内容,希望文章能够帮你解决.net – 使用Exchange Web服务发送包含特殊字符的电子邮件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1055335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存