0

我想将 MailAddressCollection 添加到我的 MailMessage(Net.Mail) 的 to、cc、bcc 和 replytolist

我的代码喜欢

   MessageEntity.To.Add(GetMailAddress(TOEmailAddress));
   MessageEntity.CC.Add(GetMailAddress(CCEmailAddress));
   MessageEntity.Bcc.Add(GetMailAddress(BCCEmailAddress));
   MessageEntity.RepltToList.Add(GetMailAddress(ReplyEmailAddress));


    private static MailAddressCollection GetMailAddress(List<string> LstMailAddress)
    {
        MailAddressCollection MAddressCollection = new MailAddressCollection();
        if (MailAddress != null)
        {
            foreach (string EmailAddress in MailAddress)
            {
                if (IsValidEmailId(EmailAddress))
                {
                    MAddressCollection.Add((new MailAddress(EmailAddress)));
                }
            }
        }
        return MAddressCollection;
    }

它显示错误无法从“System.Net.Mail.MailAddressCollection”转换为“字符串”

是否可以将 EmailAddressCollection 添加到电子邮件的 to/cc/bcc/ReplyToList?

谢谢桑

4

1 回答 1

0

通过快速重构,您可以这样做:

AddMailAddresses(MessageEntity.To, TOEmailAddress);
AddMailAddresses(MessageEntity.CC, CCEmailAddress);
AddMailAddresses(MessageEntity.Bcc, BCCEmailAddress);
AddMailAddresses(MessageEntity.ReplyToList, ReplyEmailAddress);

private static void AddMailAddresses(
    MailAddressCollection mailAddresses,
    IEnumerable<string> mailAddressesToAdd)
{
    if (mailAddressesToAdd == null)
    {
        return;
    }

    IEnumerable<string> validMailAddressesToAdd = 
        mailAddressesToAdd.Where(ma => IsValidEmailId(ma));

    foreach (string mailAddressToAdd in validMailAddressesToAdd)
    {
        mailAddresses.Add(mailAddressToAdd);
    }
}
于 2011-07-22T10:24:25.143 回答