Trying to send email to multiple recipient using below code gives me this error:
The specified string is not in the form required for an e-mail address.
string[] email = {"[email protected]","[email protected]"};
using (MailMessage mm = new MailMessage("[email protected]", email.ToString()))
{
try
{
mm.Subject = "sub;
mm.Body = "msg";
mm.Body += GetGridviewData(GridView1);
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtpout.server.net";
smtp.EnableSsl = false;
NetworkCredential NetworkCred = new NetworkCredential("email", "pass");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 80;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
}
Change your using line to this:
using (MailMessage mm = new MailMessage())
Add the from address:
mm.From = new MailAddress("[email protected]");
You can loop through your string array of e-mail addresses and add them one by one like this:
string[] email = { "[email protected]", "[email protected]", "[email protected]" };
foreach (string address in email)
{
mm.To.Add(address);
}
Example:
string[] email = { "[email protected]", "[email protected]", "[email protected]" };
using (MailMessage mm = new MailMessage())
{
try
{
mm.From = new MailAddress("[email protected]");
foreach (string address in email)
{
mm.To.Add(address);
}
mm.Subject = "sub;
// Other Properties Here
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
}
Presently your code:
new NetworkCredential("email", "pass");
treats "email"
as an email address which eventually is not an email address rather a string array which contains the email address.
So you can try like this:
foreach (string add in email)
{
mm.To.Add(new MailAddress(add));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With