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

后端 未结 9 2131
我在风中等你
我在风中等你 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:51

    My code to solve this problem:

    private void sendMail()
    {   
        //This list can be a parameter of metothd
        List lst = new List();
    
        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);
        }
    }
    

提交回复
热议问题