Hi Everyone. I want to create Emailer. In this Emailer send mail dynamically. Mailer get the LoginId and password retrieve from database and BCC ID also get from data base Each id send 50 mail on in again and again change Login id and next BCC ID send the mail.. I am doing some work this project bt i dnt get Result Still. Please help me
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
publicpartialclassDefault2 : System.Web.UI.Page
{
int t = 0;
ArrayList email_list = new ArrayList();
int i = 0;
protectedvoidPage_Load(object sender, EventArgs e)
{
}
publicvoidEmailId()
{
t = t + 1;
string UserID;
string Password;
SqlDataAdapter da;
DataTable datatable;
using (SqlConnection con = new SqlConnection("Data Source=GLASSXP;Initial Catalog=Emailer;Integrated Security=True;Pooling=False"))
{
SqlCommand cmd = new SqlCommand("select UserID,Password from Login where Id='" + t + "'", con);
da = new SqlDataAdapter(cmd);
datatable = new DataTable();
da.Fill(datatable);
UserID = Convert.ToString(datatable.Rows[0]["UserID"]);
Password = Convert.ToString(datatable.Rows[0]["Password"]);
}
}
protectedvoidbtnSendEmail_Click(object sender, EventArgs e)
{
EmailId();
DB dB = new DB();
System.Collections.Generic.List listEmailIds = dB.GetEmailID();
foreach (string email in listEmailIds)
{
{
MailMessage MyMailMessage = new MailMessage();
MyMailMessage.From = new MailAddress(UserID);
MyMailMessage.Bcc.Add(txtMail.Text);
MyMailMessage.IsBodyHtml = true;
MyMailMessage.Bcc.Add(email);
MyMailMessage.Body = txtbody.Text;
MyMailMessage.Subject = txtsubject.Text;
MyMailMessage.Priority = MailPriority.Normal;
System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(UserID, Password);
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
mailClient.Send(MyMailMessage);
try
{
mailClient.Send(MyMailMessage);
}
catch (Exception ex)
{
}
}
errorLbl.Visible = true;
errorLbl.Text = "mail sent";
}
}
}
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mahesh ChandRank #1 · 393.4K reputationPosted Feb 5, 2011, 3:16 AM EST
0
What's the error?
0
Sam HobbsRank #108 · 20.5K reputationPosted Feb 5, 2011, 10:36 AM EST
0
Your code has "mailClient.Send(MyMailMessage);" twice, correct? The first time, it is not in a try block and the second time it is in a try block. Also, the catch block that you show here does nothing, and if that is what your actual code does, then you are ignoring some critical information. You need to remove the first mailClient.Send call that is done outside the try block and then in your catch block, show the exception information.
Mahesh ChandRank #1 · 393.4K reputationPosted Feb 5, 2011, 3:16 AM EST
Sam HobbsRank #108 · 20.5K reputationPosted Feb 5, 2011, 10:36 AM EST