开发者

Add Email addressCollection to, cc, bcc and replytoList

开发者 https://www.devze.com 2023-03-21 23:48 出处:网络
i want to add MailAddressCollectionwith to,cc,bcc and replytolist of my MailMessage(Net.Mail) my code Like

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

精彩评论

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