使用EWS API发送电子邮件时控制邮件编码

时间:2016-03-22 14:07:33

标签: email encoding utf-8 exchangewebservices iso-8859-1

我正在使用Microsoft EWS(Exchange网络服务)向外部客户发送电子邮件(关于支持电话)。

我们的一位客户正在尝试自动将电子邮件正文导入他们自己的支持呼叫系统(我认为这是基于JAVA)并且他们不断获得此异常:

不受支持的编码异常:iso-8859-10

所以他们让我把它改成UTF8

如果我从我的Outlook(同一个Exchange的客户)发送电子邮件,它可以正常工作。它只发生在通过EWS发送时。

这是我的代码:

 protected void SendEmail2(string recipientsAddresses, string senderAddress, string ccRecipients, string subject, string body, bool CCToSender, bool simulate, Importance messageImportance)
    {
        ExchangeService service = null;
        EmailMessage message = null;

        try
        {
            // initialize a proxy to the exchange web services 
            service = new ExchangeService(MAGMA_EXCHAGE_VERSION);
            service.Url = new Uri(MAGMA_EWS_URI);

            // create the message
            message = new EmailMessage(service);

            // Add recipients
            foreach (string recipientEmail in recipientsAddresses.Split(";".ToCharArray()))
            {
                message.ToRecipients.Add(new EmailAddress(recipientEmail));
            }

            // handle inline images
            body = AddInlineImages(message, body);

            // set everything
            message.From = new EmailAddress(senderAddress);
            message.Subject = subject;
            message.Body = new MessageBody(BodyType.HTML, body);
            message.Importance = messageImportance;

            // and send
            message.Save();
            FolderId SentFolderForUser = new FolderId(WellKnownFolderName.SentItems, senderAddress);
            message.SendAndSaveCopy(SentFolderForUser);
        }
        catch (Exception)
        {
            throw;
        }
    }

我传递给“body”的字符串是一个包含:

的HTML 标题下的

meta charset =“utf-8”标记。

这里有什么我想念的吗?

谢谢,吉尔。

1 个答案:

答案 0 :(得分:0)

您可能希望重新编码并使用类似下面的内容来设置编码:

$ews = new ExchangeWebServices($host, $user, $password, ExchangeWebServices::VERSION_2007_SP1);
$msg = new EWSType_MessageType();
$msg->MimeContent = new EWSType_MimeContentType();
$msg->MimeContent->_ = base64_encode("Mime-Version: 1.0\r\n"
    . "From: michael@contoso.com\r\n"
    . "To: amy@contoso.com\r\n"
    . "Subject: nothing\r\n"
    . "Date: Tue, 15 Feb 2011 22:06:21 -0000\r\n"
    . "Message-ID: <{0}>\r\n"
    . "X-Experimental: some value\r\n"
    . "\r\n"
    . "I have nothing further to say.\r\n");
$msg->MimeContent->CharacterSet = 'UTF-8';

另外请看这个question here,因为它接近相似。

注意:这是一个很好的starting point regarding the content-type encoding选项。您也可以查看官方Microsoft howto here