我很难收到电子邮件的收件人。
我知道收件人是一个数组,所以我需要把它们放到一个数组中,但我的代码不能编译:
do
{
// set the prioperties we need for the entire result set
view.PropertySet = new PropertySet(
BasePropertySet.IdOnly,
ItemSchema.Subject,
ItemSchema.DateTimeReceived,
ItemSchema.DisplayTo, EmailMessageSchema.ToRecipients,
EmailMessageSchema.From, EmailMessageSchema.IsRead,
EmailMessageSchema.HasAttachments, ItemSchema.MimeContent,
EmailMessageSchema.Body, EmailMessageSchema.Sender,
ItemSchema.Body) { RequestedBodyType = BodyType.Text };
// load the properties for the entire batch
service.LoadPropertiesForItems(results, view.PropertySet);
e2cSessionLog("\tcommon.GetUnReadMailAll", "retrieved " + results.Count() + " emails from Mailbox (" + common.strInboxURL + ")");
foreach (EmailMessage email in results)
// looping through all the emails
{
emailSenderName = email.From.Address;
sEmailSubject = email.Subject;
emailDateTimeReceived = email.DateTimeReceived.ToShortDateString();
emailHasAttachments = email.HasAttachments;
ItemId itemId = email.Id;
emailDisplayTo = email.DisplayTo;
sEmailBody = email.Body; //.Text;
Recipients = email.ToRecipients;
....
最后一行不会编译,因为显然我无法将集合ToRecipients隐式转换为字符串...
所以我试图遍历所有ToRecipients:
string[] Recipients;
for (int iIdx=0; iIdx<-email.ToRecipients.Count; iIdx++)
{
Recipients[iIdx] = email.ToRecipients[iIdx].ToString();
}
但我显然没有正确地声明这一点,因为它不会使用Recipients
未分配的消息进行编译。
分配这个的正确方法是什么?
我需要以后能够再次使用收件人 - 例如,向他们发送“收件人”。有关问题的电子邮件,例如。
答案 0 :(得分:3)
您需要正确初始化数组,并且需要使用ToRecipient的Address属性:
var Recipients = new string[email.ToRecipients.Count];
for (int iIdx = 0; iIdx < email.ToRecipients.Count; iIdx++) {
Recipients[iIdx] = email.ToRecipients[iIdx].Address;
}
BTW,我认为你的伪代码中有一个拼写错误:
for(...; iIdx<-email.ToRecipients.Count; ...) {
在那里你有一个减-
,这将导致没有迭代,因为第一次迭代不会通过(0&lt; -count是false
)。我想你的意思是
for(...; iIdx < email.ToRecipients.Count; ...) {
更简单,更不容易出错的解决方案是:
var recipients = email.ToRecipients
.Select(x => x.Address)
.ToList(); // or ToArray()