How to send email to multiple address using System.Net.Mail

后端 未结 9 2030
我在风中等你
我在风中等你 2020-11-28 09:03

I have smtp email functionality. it works for single address but has problem in multiple address.

i am passing multiple addresses using following line of code.

相关标签:
9条回答
  • 2020-11-28 09:47
    MailMessage msg = new MailMessage();
    msg.Body = ....;
    msg.To.Add(...);
    msg.To.Add(...);
    
    SmtpClient smtp = new SmtpClient();
    smtp.Send(msg);
    

    To is a MailAddressCollection, so you can add how many addresses you need.

    If you need a display name, try this:

    MailAddress to = new MailAddress(
        String.Format("{0} <{1}>",display_name, address));
    
    0 讨论(0)
  • 2020-11-28 09:47
    namespace WebForms.Code.Logging {
    
        public class ObserverLogToEmail: ILog {
            private string from;
            private string to;
            private string subject;
            private string body;
            private SmtpClient smtpClient;
            private MailMessage mailMessage;
            private MailPriority mailPriority;
            private MailAddressCollection mailAddressCollection;
            private MailAddress fromMailAddress, toMailAddress;
            public MailAddressCollection toMailAddressCollection {
                get;
                set;
            }
            public MailAddressCollection ccMailAddressCollection {
                get;
                set;
            }
            public MailAddressCollection bccMailAddressCollection {
                get;
                set;
            }
    
            public ObserverLogToEmail(string from, string to, string subject, string body, SmtpClient smtpClient) {
                this.from = from;
                this.to = to;
                this.subject = subject;
                this.body = body;
    
                this.smtpClient = smtpClient;
            }
    
            public ObserverLogToEmail(MailAddress fromMailAddress, MailAddress toMailAddress,
            string subject, string content, SmtpClient smtpClient) {
                try {
                    this.fromMailAddress = fromMailAddress;
                    this.toMailAddress = toMailAddress;
                    this.subject = subject;
                    this.body = content;
    
                    this.smtpClient = smtpClient;
    
                    mailAddressCollection = new MailAddressCollection();
    
                } catch {
                    throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
                }
            }
    
            public ObserverLogToEmail(MailAddressCollection fromMailAddressCollection,
            MailAddressCollection toMailAddressCollection,
            string subject, string content, SmtpClient smtpClient) {
                try {
                    this.toMailAddressCollection = toMailAddressCollection;
                    this.ccMailAddressCollection = ccMailAddressCollection;
                    this.subject = subject;
                    this.body = content;
    
                    this.smtpClient = smtpClient;
    
                } catch {
                    throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
                }
            }
    
            public ObserverLogToEmail(MailAddressCollection toMailAddressCollection,
            MailAddressCollection ccMailAddressCollection,
            MailAddressCollection bccMailAddressCollection,
            string subject, string content, SmtpClient smtpClient) {
                try {
                    this.toMailAddressCollection = toMailAddressCollection;
                    this.ccMailAddressCollection = ccMailAddressCollection;
                    this.bccMailAddressCollection = bccMailAddressCollection;
    
                    this.subject = subject;
                    this.body = content;
    
                    this.smtpClient = smtpClient;
    
                } catch {
                    throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
                }
            }#region ILog Members
    
            // sends a log request via email.
            // actual email 'Send' calls are commented out.
            // uncomment if you have the proper email privileges.
            public void Log(object sender, LogEventArgs e) {
                string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message;
                fromMailAddress = new MailAddress("", "HaNN", System.Text.Encoding.UTF8);
                toMailAddress = new MailAddress("", "XXX", System.Text.Encoding.UTF8);
    
                mailMessage = new MailMessage(fromMailAddress, toMailAddress);
                mailMessage.Subject = subject;
                mailMessage.Body = body;
    
                // commented out for now. you need privileges to send email.
                // _smtpClient.Send(from, to, subject, body);
                smtpClient.Send(mailMessage);
            }
    
            public void LogAllEmails(object sender, LogEventArgs e) {
                try {
                    string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message;
    
                    mailMessage = new MailMessage();
                    mailMessage.Subject = subject;
                    mailMessage.Body = body;
    
                    foreach(MailAddress toMailAddress in toMailAddressCollection) {
                        mailMessage.To.Add(toMailAddress);
    
                    }
                    foreach(MailAddress ccMailAddress in ccMailAddressCollection) {
                        mailMessage.CC.Add(ccMailAddress);
                    }
                    foreach(MailAddress bccMailAddress in bccMailAddressCollection) {
                        mailMessage.Bcc.Add(bccMailAddress);
                    }
                    if (smtpClient == null) {
                        var smtp = new SmtpClient {
                            Host = "smtp.gmail.com",
                            Port = 587,
                            EnableSsl = true,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            Credentials = new NetworkCredential("yourEmailAddress", "yourPassword"),
                            Timeout = 30000
                        };
                    } else smtpClient.SendAsync(mailMessage, null);
                } catch (Exception) {
    
                    throw;
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-28 09:51

    My code to solve this problem:

    private void sendMail()
    {   
        //This list can be a parameter of metothd
        List<MailAddress> lst = new List<MailAddress>();
    
        lst.Add(new MailAddress("mouse@xxxx.com"));
        lst.Add(new MailAddress("duck@xxxx.com"));
        lst.Add(new MailAddress("goose@xxxx.com"));
        lst.Add(new MailAddress("wolf@xxxx.com"));
    
    
        try
        {
    
    
            MailMessage objeto_mail = new MailMessage();
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.Host = "10.15.130.28"; //or SMTP name
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("from@email.com", "password");
            objeto_mail.From = new MailAddress("from@email.com");
    
            //add each email adress
            foreach (MailAddress m in lst)
            {
                objeto_mail.To.Add(m);
            }
    
    
            objeto_mail.Subject = "Sending mail test";
            objeto_mail.Body = "Functional test for automatic mail :-)";
            client.Send(objeto_mail);
    
    
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    0 讨论(0)
提交回复
热议问题