Exchange Web Services - 将emailitem附件从Base64字符串转换为字节给出错误 [英] Exchange Web Services - convert emailitem attachment from Base64 string to Byte gives error
问题描述
我正在尝试使用EWS读取电子邮件附件,并将其另存为磁盘作为文本文件,以便稍后使用。
I am trying to read an email item attachment using EWS and save it to disk as a text file so it can be used later on.
我正在收到错误:
"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. "
这是我的代码:
Directory.CreateDirectory(emailAttachmentsPath);
// Put attachment contents into a stream. C:\Dev\EWSHelloWorld
emailAttachmentsPath = emailAttachmentsPath + "\\" + sEmailSubject+".txt";
//save to disk
using (Stream FileToDisk = new FileStream(emailAttachmentsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
byte[] ContentBytes = System.Convert.FromBase64String(itemAttachment.ToString());
FileToDisk.Write(ContentBytes, 0,ContentBytes.Length);
FileToDisk.Flush();
FileToDisk.Close();
}
最好的方法是什么?
我基本上想要在文本文件中的电子邮件的文本,并且该电子邮件中的任何附件也将保存到磁盘(我可以做这个部分我认为使用FileStream。
I basically want the text of the email in a text file, and any attachments in that email would be saved to disk as well (I can do that part I think using FileStream.
推荐答案
Philip,
您将无法使用 Convert()
方法在 ItemAttachment 中,因为它不是Base64编码的。项目附件有很多关于该项目的属性,如果我理解你的请求正确正在寻找电子邮件的正文。
You will not be able to use the Convert()
method on an ItemAttachment because it is not Base64 encoded. The item attachment has a lot of properties about the item, and if I understand your request properly you are looking for just the body of the email.
您想要考虑的第一件事是添加一个检查来查看ItemAttachment是否是电子邮件消息,如果是,有几行可以获取电子邮件正文的文本:
The first thing you will want to consider is adding a check to see if the ItemAttachment is an email message. If it is, there are couple of lines to get to the text of the email body:
itemAttachment.Load(new PropertySet(BasePropertySet.FirstClassProperties));
string BodyText = itemAttachment.Item.Body.Text;
第一行将加载项,它是第一类属性。第二行将获得电子邮件正文的文本版本。
The first line will load the item and it's first class properties. The second line will get the text version of the email body.
希望这有帮助。如果这样做解决了您的问题,请将帖子标记为已回覆。
I hope this helps. If this does resolve your question, please mark the post as answered.
谢谢,
--- Bob ---
--- Bob ---
这篇关于Exchange Web Services - 将emailitem附件从Base64字符串转换为字节给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!