将电子邮件地址集合添加到、抄送、密件抄送和回复列表中

发布于 2024-11-26 02:54:14 字数 1034 浏览 2 评论 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”转换为“string”

是否可以添加EmailAddressCollection 发送电子邮件至/cc/bcc/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)));
                }
            }
        }
        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-12-03 02:54:14

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

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文