user5278314
user5278314

Reputation:

sending email more than once at a time

I want to send an email to more than 1 person. Why this code is showing this message: "the parameter'to' can not be an empty string Parameter name:to" ?

connection.Open();
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
string cq = "Select Email From student where S_ID in(select S_ID FROM studentbook where DateDiff('d',[Issue_Date], NOW())=31) ";
command1.CommandText = cq;
OleDbDataAdapter da1 = new OleDbDataAdapter(command1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);

foreach (DataRow row in dt1.Rows)
{
    string email = row["Email"].ToString();
    MessageBox.Show("Trying to send email ");

    using (MailMessage mm = new MailMessage("[email protected]", email))
    {
        mm.Subject = "Attention please,renew your book";
        mm.Body = string.Format("1 month over,you should renew or return the book");

        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
        credentials.UserName = "xxxxxxxxxxxxx";
        credentials.Password = "xxxxxxxxxx";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = credentials;
        smtp.Port = 587;
        smtp.Send(mm);
        MessageBox.Show("Email sent successfully");
    }
}

Upvotes: -1

Views: 99

Answers (4)

H. Pauwelyn
H. Pauwelyn

Reputation: 14320

Try this:

mm.To.Add(new MailAddress("[email protected]"));
mm.To.Add(new MailAddress("[email protected]"));

Don't forget to add an address where you sending from:

mm.From = new MailAddress("[email protected]"));

If you will send e-mails in BCC you can use:

mm.Bcc.Add(new MailAddress("[email protected]"));
mm.Bcc.Add(new MailAddress("[email protected]"));

Source: msdn.microsoft.com

Upvotes: 0

WAQ
WAQ

Reputation: 2626

I guess the email that you are getting from datatable might be null or empty. So make sure to check for null/empty string before proceeding with sending the email.

if(String.IsNullOrEmpty(value))
    continue;

Also, If you are sending the same email to multiple recipients, then instead of doing everything in the foreach loop, add the recipients in mm.To.Add("email address to send to"); and then finally send the email.

connection.Open();
varcommand1 = new OleDbCommand();
command1.Connection = connection;
varcq = "Select Email From student where S_ID in(select S_ID FROM studentbook where DateDiff('d',[Issue_Date], NOW())=31) ";
command1.CommandText = cq;
varda1 = new OleDbDataAdapter(command1);
vardt1 = new DataTable();
da1.Fill(dt1);
using (var mm = new MailMessage())
{
    foreach (DataRow row in dt1.Rows)
    {
        varemail = row["Email"].ToString();
        if(string.IsNullOrWhitespace(email))
            continue;
        mm.Bcc.Add(email);   
    }
    mm.From.Add("SenderEmailAddress");   
    mm.Subject = "Attention please,renew your book";
    mm.Body = string.Format("1 month over,you should renew or return the book");

    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    var credentials = new System.Net.NetworkCredential();
    credentials.UserName = "xxxxxxxxxxxxx";
    credentials.Password = "xxxxxxxxxx";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = credentials;
    smtp.Port = 587;
    smtp.Send(mm);
}

Upvotes: 0

TheTerribleProgrammer
TheTerribleProgrammer

Reputation: 546

After getting the email address from the datatable, do a check to see if the email is not empty. If it is empty continue to the next row.

string email = row["Email"].ToString();
if(String.IsNullOrEmpty(value) || value.Trim().Length == 0)
{
    continue;
}

Upvotes: 2

Nevca
Nevca

Reputation: 204

You have to add at least one recipient:

mm.To.Add("[email protected]");

You can also add multiple recipients.

Upvotes: 0

Related Questions