Error trying to send mail. Not sending using EWS

拥有回忆 提交于 2019-12-11 18:38:10

问题


I'm connecting fine to the Exchange Server and I'm getting all the unread mails but a new message doesn't want to send

Message Code

var newHTML = html.HTMLCode.Replace("{House}", house.Number)
                           .Replace("{Token}", token.Number)
                           .Replace("{contactPerson}", 
                                    string.Format("<a href=mailto:{0}>{1}</a>", 
                                    contactPerson, contactPerson));
LogError.WriteToFile("Has House and token");
//Send mail with token to user 
EmailMessage message = new EmailMessage(emailService);
message.ToRecipients.Add(email.From.Address);
message.Subject = string.Format("Electricity token for: {0}", house.Number);
message.Body = new MessageBody(html.HTMLCode);
LogError.WriteToFile("Trying to send");
message.Send();

I have a try catch around this so in the log file I get "Trying to send" but then a error occurs that reads as "EmailAddress or ItemId must be included in the request."

From examples seen, the way I construct my message seems sufficient but clearly isn't


回答1:


This is how I got all my unread emails

 SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            ItemView view = new ItemView(int.MaxValue);
            FindItemsResults<Item> findResults = emailService.FindItems(WellKnownFolderName.Inbox, sf, view);
            foreach (EmailMessage email in findResults)
            {}

Note that I got them as a "EmailMessage"

But were never able to get the senders email address so that's why it didn't want to send. and then I found this article: FindItem returns only the first 512 bytes of any streamable property

So then I went to go get that specific email like this. Note that I now get the "Item" from the findResults and with that "Item" I do the following.

SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            ItemView view = new ItemView(int.MaxValue);
            FindItemsResults<Item> findResults = emailService.FindItems(WellKnownFolderName.Inbox, sf, view);
            foreach (Item item in findResults)
            {
//Get the email message
                    EmailMessage email = EmailMessage.Bind(emailService, item.Id,
                    new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));

                    if (email != null)
                    {}
}

Now I could finally send emails



来源:https://stackoverflow.com/questions/24427044/error-trying-to-send-mail-not-sending-using-ews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!