尝试发送邮件时出错。不使用EWS发送

时间:2014-06-26 09:34:23

标签: c# exchangewebservices

我正好连接到Exchange Server,而且我收到了所有未读邮件,但是新邮件并不想发送

消息代码

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();

我试着抓住这个,所以在我得到的日志文件中&#34;尝试发送&#34;但随后出现错误,读作为 &#34; EmailAddress或ItemId必须包含在请求中。&#34;

从看到的例子来看,我构建我的信息的方式似乎已经足够,但显然不是

1 个答案:

答案 0 :(得分:0)

这就是我收到所有未读电子邮件的方式

 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)
            {}

请注意,我将它们作为&#34; EmailMessage&#34;

但是从来没有能够获得发件人的电子邮件地址,这就是为什么它不想发送。 然后我发现了这篇文章:FindItem returns only the first 512 bytes of any streamable property

然后我去了这样的特定邮件。请注意,我现在得到&#34; Item&#34;来自findResults和那个&#34; Item&#34;我做了以下几点。

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)
                    {}
}

现在我终于可以发送电子邮件了