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"] + ",";
}