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

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

    try this..

    using System;
    using System.Net.Mail;
    
    public class Test
    {
        public static void Main()
        {
            SmtpClient client = new SmtpClient("smtphost", 25);
            MailMessage msg = new MailMessage("x@y.com", "a@b.com,c@d.com");
            msg.Subject = "sdfdsf";
            msg.Body = "sdfsdfdsfd";
            client.UseDefaultCredentials = true;
            client.Send(msg);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:36

    I'm used "for" operator.

    try
    {
        string s = textBox2.Text;
        string[] f = s.Split(',');
    
        for (int i = 0; i < f.Length; i++)
        {
            MailMessage message = new MailMessage(); // Create instance of message
            message.To.Add(f[i]); // Add receiver
            message.From = new System.Net.Mail.MailAddress(c);// Set sender .In this case the same as the username
            message.Subject = label3.Text; // Set subject
            message.Body = richTextBox1.Text; // Set body of message
            client.Send(message); // Send the message
            message = null; // Clean up
        }
    
    }
    
    catch (Exception ex)
    {
    
        MessageBox.Show(ex.Message);
    }
    
    0 讨论(0)
  • 2020-11-28 09:37

    StewieFG suggestion is valid but if you want to add the recipient name use this, with what Marco has posted above but is email address first and display name second:

    msg.To.Add(new MailAddress("your@email1.com","Your name 1"));
    msg.To.Add(new MailAddress("your@email2.com","Your name 2"));
    
    0 讨论(0)
  • 2020-11-28 09:37
     string[] MultiEmails = email.Split(',');
     foreach (string ToEmail in MultiEmails)
     {
        message.To.Add(new MailAddress(ToEmail)); //adding multiple email addresses
     }
    
    0 讨论(0)
  • 2020-11-28 09:39
    MailAddress fromAddress = new MailAddress  (fromMail,fromName);
    MailAddress toAddress = new MailAddress(toMail,toName);
    MailMessage message = new MailMessage(fromAddress,toAddress);
    message.Subject = subject;
    message.Body = body;
    SmtpClient smtp = new SmtpClient()
    {
        Host = host, Port = port,
        enabHost = "smtp.gmail.com",
        Port = 25,
        EnableSsl = true,
        UseDefaultCredentials = true,
        Credentials = new  NetworkCredentials (fromMail, password)
    };
    smtp.send(message);
    
    0 讨论(0)
  • 2020-11-28 09:44

    I think you can use this code in order to have List of outgoing Addresses having a display Name (also different):

    //1.The ACCOUNT
    MailAddress fromAddress = new MailAddress("myaccount@myaccount.com", "my display name");
    String fromPassword = "password";
    
    //2.The Destination email Addresses
    MailAddressCollection TO_addressList = new MailAddressCollection();
    
    //3.Prepare the Destination email Addresses list
    foreach (var curr_address in mailto.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
    {
        MailAddress mytoAddress = new MailAddress(curr_address, "Custom display name");
        TO_addressList.Add(mytoAddress);
    }
    
    //4.The Email Body Message
    String body = bodymsg;
    
    //5.Prepare GMAIL SMTP: with SSL on port 587
    var smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
        Timeout = 30000
    };
    
    
    //6.Complete the message and SEND the email:
    using (var message = new MailMessage()
            {
                From = fromAddress,
                Subject = subject,
                Body = body,
            })
    {
        message.To.Add(TO_addressList.ToString());
        smtp.Send(message);
    }
    
    0 讨论(0)
提交回复
热议问题