Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c# - How to send an email?

I have an datatable like this.

I have an Excel sheet like this. Now I am reading the data from that and converting into an datatable like this:

id   Name     MailID                Body

123  kirna    [email protected]     happy birthday   
234  ram      [email protected]       happy birthday  
345  anu      [email protected]    how is the day going
357  rashmi   [email protected]    work need  to be completed

Now I to send email to all the above person.

Can any one help me how I can read data from datatable and send mail to them with the body what is been specified.

Any help would be great.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use the SmtpClient class:

foreach (DataRow row in datatable.Rows)
{
    var name = (string)row["Name"];
    var email = (string)row["MailID"];
    var body = (string)row["Body"];

    var message = new MailMessage();
    message.To.Add(email);
    message.Subject = "This is the Subject";
    message.From = new MailAddress("[email protected]");
    message.Body = body;
    var smtpClient = new SmtpClient("yoursmtphost");
    smtpClient.Send(message);
}

Remark1: In .NET 4.0, SmtpClient implements IDisposable, so make sure to properly dispose it.

Remark2: There's a bug in SmtpClient class prior to .NET 4.0 which doesn't properly send the QUIT command to the SMTP server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...