Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EWS获取电子邮件收件人_C#_Arrays_Email_Collections_Exchangewebservices - Fatal编程技术网

C# 如何使用EWS获取电子邮件收件人

C# 如何使用EWS获取电子邮件收件人,c#,arrays,email,collections,exchangewebservices,C#,Arrays,Email,Collections,Exchangewebservices,我正在努力获取电子邮件的收件人 我知道收件人是一个数组,所以我需要将他们放入数组中,但我的代码不会编译: do { // set the prioperties we need for the entire result set view.PropertySet = new PropertySet( BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived, ItemSchema

我正在努力获取电子邮件的收件人

我知道收件人是一个数组,所以我需要将他们放入数组中,但我的代码不会编译:

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();
}
但我显然没有正确地声明这一点,因为它不会编译收件人未分配的消息

正确的分配方式是什么


我需要能够在以后再次使用收件人-例如,向他们发送一封有关问题的“提示”电子邮件。

您需要正确初始化数组,并且需要使用ToRecipient的Address属性:

var Recipients = new string[email.ToRecipients.Count];
for (int iIdx = 0; iIdx < email.ToRecipients.Count; iIdx++) {
    Recipients[iIdx] = email.ToRecipients[iIdx].Address;
}
使现代化 一个更简单、更不容易出错的解决方案是:

var recipients = email.ToRecipients
    .Select(x => x.Address)
    .ToList(); // or ToArray()

+谢谢,也谢谢你的打字错误,最后我选择了使用列表,但这也解决了问题!
for(...; iIdx < email.ToRecipients.Count; ...) {
var recipients = email.ToRecipients
    .Select(x => x.Address)
    .ToList(); // or ToArray()