I have a sql-view which contains some simple data and also an column with an emailadres. When i click a button i want to send a mail to every distinct emailadres with an export of the view containing only the rows with that specific emailadres.
Everthing is going well except that the mails always get the same attachment! I guess it has something to do with the way I subcribe to the grid exporting event?
This is my code:
Hopefully anyone can help, that would be a lifesaver!
Thanks
Everthing is going well except that the mails always get the same attachment! I guess it has something to do with the way I subcribe to the grid exporting event?
This is my code:
protected
void
rbTestMailVersturen_Click(
object
sender, EventArgs e) {
string
query =
""
;
teller = 0;
SaveMailingForm();
RadGrid emailgrid =
new
RadGrid();
PlaceHolder1.Controls.Add(emailgrid);
//Fill a list with emailadresses
FillEmailadressen();
for
(
int
i = 0; i < emailadressen.Count; i++) {
{
using
(conn =
new
SqlConnection(ConnectionString)) {
conn.Open();
emailgrid.DataSource =
null
;
emailgrid.Rebind();
string
ID = Request.QueryString[
"id"
].PadLeft(2,
'0'
);
query =
"SELECT * FROM A_MAN_"
+ ID +
" WHERE EMAILCOLUMN ='"
+ emailadressen[i] +
"'"
;
SqlDataAdapter da =
new
SqlDataAdapter(query, conn);
DataTable dt =
new
DataTable();
da.Fill(dt);
emailgrid.DataSource = dt;
emailgrid.Rebind();
emailgrid.GridExporting +=
new
OnGridExportingEventHandler(emailgrid_GridExporting);
switch
(rcbExportType.SelectedItem.Text) {
case
"CSV"
:
emailgrid.MasterTableView.ExportToCSV();
break
;
case
"Excel"
:
emailgrid.MasterTableView.ExportToExcel();
break
;
case
"PDF"
:
emailgrid.MasterTableView.ExportToPdf();
break
;
case
"Word"
:
emailgrid.MasterTableView.ExportToWord();
break
;
}
}
}
}
}
protected
void
emailgrid_GridExporting(
object
source, GridExportingArgs e) {
string
filename = DateTime.Now.Year +
"-"
+ DateTime.Now.Month +
"-"
+ DateTime.Now.Day + rtbOnderwerp.Text + teller +
"."
;
switch
(rcbExportType.SelectedItem.Text) {
case
"CSV"
:
filename +=
"csv"
;
break
;
case
"Excel"
:
filename +=
"xls"
;
break
;
case
"PDF"
:
filename +=
"pdf"
;
break
;
case
"Word"
:
filename +=
"doc"
;
break
;
}
string
path = Server.MapPath(
"~/"
+ filename);
using
(FileStream fs = File.Create(path)) {
Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
fs.Write(info, 0, info.Length);
}
MailMessage msg =
new
MailMessage();
MailAddress From =
new
MailAddress(
"info@xxxxxxxxxxxx.nl"
,
"info@xxxxxxxxxxxx.nl"
);
msg.From = From;
msg.Subject = rtbOnderwerp.Text;
msg.Body = rtbMailtekst.Text;
msg.To.Add(
new
MailAddress(emailadressen[teller]));
teller++;
Attachment at =
new
Attachment(Server.MapPath(
"~/"
+ filename));
msg.Attachments.Add(at);
SmtpClient client =
new
SmtpClient(
"localhost"
);
client.Send(msg);
msg.Dispose();
FileInfo fi =
new
FileInfo(Server.MapPath(
"~/"
+ filename));
fi.Delete();
if
(teller == emailadressen.Count)
Response.Redirect(Request.Url.ToString());
}
Hopefully anyone can help, that would be a lifesaver!
Thanks