0

I have the following code inside my asp.net mvc-5 web application:-

string from = "[email protected]"; 
                using (MailMessage mail = new MailMessage(from, "[email protected]"))
                {

currently when i try to add multiple recipients for the email as follow :-

string from = "[email protected]"; 
                using (MailMessage mail = new MailMessage(from, "[email protected];[email protected]"))
                {

it will not send any emails, so can anyone advice on this please ?

  • 2
    _`new MailMessage(from, "[email protected];[email protected]")`_ What you are trying to do is using **`from`**, how can there be **2 froms** for a single mail? Check this post to know **[how to add multiple receipients](http://stackoverflow.com/questions/23484503/sending-email-to-multiple-recipients-with-mailmessage)** – Guruprasad J Rao Sep 21 '15 at 12:18
  • According to this [thread](http://stackoverflow.com/q/9736176/728795), what you are trying to do is not fully supported – Andrei Sep 21 '15 at 12:21
  • @GuruprasadRao i am not trying to send email from multiple From, i want multiple Tos ?? –  Sep 21 '15 at 12:21
  • instead of semicolon(;) try comma(,) – Ubiquitous Developers Sep 21 '15 at 12:23
  • @JohnJohn With the above code you're trying to send a mail from multiple froms, look at Amit's answer how to do it right – Sybren Sep 21 '15 at 12:24
  • @JohnJohn - _currently when i try to add multiple recipients for the email as follow_ the code below above statement were trying to achieve what I said.. Anyways.. The link I've given shows how to add multiple receipients.. – Guruprasad J Rao Sep 21 '15 at 12:24

3 Answers3

2

Try this -

        string from = "[email protected]"; 
        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress(from);
            mail.To.Add("[email protected]");
            mail.To.Add("[email protected]");
        }

The new MailMessage() constructor takes arguments for both from and to, and should be used when you have just single recipient. In case of multiple recipient, you have to add them to the mail.To collection

Yogi
  • 9,174
  • 2
  • 46
  • 61
1

Try ewith following namespaces.

string from = [email protected];
string to = [email protected];
string to1 [email protected];

mail.From = new System.Net.Mail.MailAddress(from);
mail.To.Add(new System.Net.Mail.MailAddress(to));
mail.To.Add(new System.Net.Mail.MailAddress(to1));

MailMessage take a single reciver for to

public MailMessage( MailAddress from, MailAddress to )

Check here

So you have to add objects for others recivers in to list

Amit Soni
  • 3,216
  • 6
  • 31
  • 50
0
public class MailHandler
{

public bool SendEMail(string smtpHost ="smtp.gmail.com", int port = 587, string senderMail , string  senderPass, ArrayList mailToArr, string subject, bool isHtml, string body)
    {
        try
        {

            SmtpClient smtpClient = new SmtpClient(smtpHost, port);
            smtpClient.UseDefaultCredentials = false;// true;
            smtpClient.Credentials = new System.Net.NetworkCredential(senderMail, senderPass);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = true;

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(senderMail);
            for (int i = 0; i < mailToArr.Count; i++)
            {
                mail.To.Add(new MailAddress((string)mailToArr[i]));
            }
            mail.Subject = subject;
            //mail.CC.Add(new MailAddress("[email protected]"));
            mail.Body = body;
            mail.IsBodyHtml = isHtml;
            mail.Priority = MailPriority.Normal;

            smtpClient.Send(mail);

            return true;
        }
        catch (Exception ex)
        {
            return false;

            // write exception on server log

        }

    }
}

//to call function

                ArrayList mailToArr = new ArrayList();
                mailToArr.Add("to first email address");
                mailToArr.Add("to second email address");
                mailToArr.Add("to third email address");

                MailHandler objMailhandler = new MailHandler();
                string subject = "subject";
                string body = "<h1>  Password Request </h1> " ;


                objMailhandler.SendEMail("smtp.gmail.com", 587, senderMail , senderPass,mailToArr, subject, true, body);
Mohammad Shraim
  • 1,173
  • 12
  • 21