开发者

Add Email addressCollection to, cc, bcc and replytoList

i want to add MailAddressCollection with to,cc,bcc and replytolist of my MailMessage(Net.Mail)

my code Like

   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)));
                }
            }
        }开发者_如何学Python
        return MAddressCollection;
    }

It is showing the error cannot convert from 'System.Net.Mail.MailAddressCollection' to 'string'

Is it possible to add the EmailAddressCollection to email's to/cc/bcc/ReplyToList?

Thanks San


With a quick refactor, you can do it like this:

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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜