By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Problem in getting email id's separated by comma

P: 3
Hi,
I'm using VS.Net 2005
and MS Access


I'm using the following code for sending email......
the problem is I've to extract email id's from database and put them in TO or CC field separated with comma when Page is loaded

plz help me in this regard

// message elements
private string mMailServer;
private string mTo;
private string mFrom;
private string mMsg;
private string mSubject;
private string mCC;
private int mPort;



override protected void OnInit(EventArgs e)
{

base.OnInit(e);
btnSend.Click += new System.EventHandler(btnSend_Click);
}

private void MessageBox(string strMsg)
{
Label lbl = new Label();
lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert(" + "'" + strMsg + "'" + ")</script>";

Page.Controls.Add(lbl);

}



protected void btnSend_Click(object sender, System.EventArgs e)
{
string strMsg = null;
strMsg = txtMessage.Text;

System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OleDb.4.0;Data Source=c:/mms.mdb");
con.Open();

mTo = txtTo.Text.Trim(' ');
mFrom = txtFrom.Text.Trim(' ');
mSubject = txtSubject.Text.Trim(' ');
mMsg = txtMessage.Text.Trim(' ');
mMailServer = ConfigurationManager.AppSettings.Get("localhost");
mPort = 25;
mCC = txtCC.Text.Trim(' ');

try
{

MailMessage message = new MailMessage(mFrom, mTo, mSubject, mMsg);

if (fileAttachments.HasFile)
{
Attachment attached = new Attachment(fileAttachments.PostedFile.FileName.ToS tring().Trim(' '));
message.Attachments.Add(attached);
}

if (mCC != "" | mCC != string.Empty)
{
string[] strCC = mCC.Split(';');
foreach (string strThisCC in strCC)
{
message.CC.Add(strThisCC.Trim(' '));
}
}


SmtpClient mySmtpClient = new SmtpClient(mMailServer, mPort);
mySmtpClient.UseDefaultCredentials = true;
mySmtpClient.Send(message);

MessageBox("The mail message has been sent to " + message.To.ToString());

}
catch (FormatException ex)
{

MessageBox("Format Exception: " + ex.Message);

}
catch (SmtpException ex)
{

MessageBox("SMTP Exception: " + ex.Message);

}
catch (Exception ex)
{

MessageBox("General Exception: " + ex.Message);

}

}





In PageLoad I'm currently using the following code


System.Data.OleDb.OleDbConnection con;
System.Data.OleDb.OleDbCommand cmd1;
System.Data.OleDb.OleDbDataAdapter adap;
DataSet ds = new DataSet();


string sql = string.format("select email from emp);
adap = new System.Data.OleDb.OleDbDataAdapter(cmd);
for (I = 0; I < ds.Tables["emp"]. Rows.Count; I++)
{
txtTO.Text = ds.Tables["emp"]. Rows[i]["email"] + ",";
}
May 19 '07 #1
Share this Question
Share on Google+
2 Replies


shweta123
Expert 100+
P: 692
Hi,

Are you getting any error?
Otherwise ,Your code seems to be ok.
You ,can check for the null value of email before this

txtTO.Text = ds.Tables["emp"]. Rows[i]["email"] + ",";.
May 20 '07 #2

P: 3
Hi,

Are you getting any error?
Otherwise ,Your code seems to be ok.
You ,can check for the null value of email before this

txtTO.Text = ds.Tables["emp"]. Rows[i]["email"] + ",";.

Hi, I'm getting Object reference not set to an instance of an object. error in for loop

for (int I = 0; I < ds.Tables["emp"]. Rows.Count; I++)
{
txtTo.Text = ds.Tables["emp"]. Rows[i]["email"] + ",";
}







How to send email to my own system and see it ???
May 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.