Windows programming
how to send bulk main in C#?...I have one button and textbox for email address and i want to get email address from excel file --like 200 email address....in excel file----thn a single mail send to all email adress --with body?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jean PaulRank #32 · 38.5K reputationPosted Nov 16, 2010, 7:29 PM EST
SmtpClient smtpClient = new SmtpClient("yourmailserver.com", 25);
smtpClient.UseDefaultCredentials = true;
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("[email protected]", "Your Display Name");
message.To.Add("[email protected],[email protected],[email protected]"); // Add multiple mail addresses here
message.From = fromAddress;
message.Subject = "Your Subject";
message.Body = "The Message Body";
smtpClient.Send(message);
Deepak SharmaRank #541 · 4.5K reputationPosted Nov 16, 2010, 9:32 PM EST
Below is the code that takes email ids from an Excel file and send emails to all the email ids with textBox1 text as message body. It uses a Gmail account to send emails. You can use other email account with their corresponding smtp client and other details.
Add reference to "Microsoft.Office.Interop.Excel" using Add Reference dialog box.
"If it helped you. Check the Do you like this Answer Check Box at the top."
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
namespace Email
{
public partial class eMail : Form
{
String Email;
String SheetName = "Sheet1"; // Name of the sheet in the Excel file
String ConString;
MailMessage message;
OleDbConnection con;
OleDbCommand cmd;
OleDbDataReader reader;
SmtpClient smtp;
public eMail()
{
InitializeComponent();
// Email.xls file is stored in the Debug folder. Emails are stored in Sheet1 under heading Email
ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Email.xls;Extended Properties=Excel 8.0";
}
private void button1_Click(object sender, EventArgs e)
{
message = new MailMessage();
con = new OleDbConnection(ConString);
con.Open();
cmd = new OleDbCommand(" SELECT Email FROM [" + SheetName + "$]", con);
reader = cmd.ExecuteReader();
while (reader.Read())
{
Email= reader[0].ToString();
message.To.Add(Email.Trim());
}
reader.Close();
con.Close();
message.Subject = "Bulk Email using C#.net";
message.From = new MailAddress("fromemailaddress"); //[email protected]
message.Body = textBox1.Text;
smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("("fromemailaddress");", "password");
smtp.Send(message);
}
}
}