c# - Sending Datagridview to email -
i made code can send email gmail.com c# , working well. want put datagridview1 in email body , send it. somone can show me how can that? searched lot found useless information , asp.net codes.
here atual code of sending email . datagridview name : datagridview1
private void btnsend_click_1(object sender, eventargs e) { // create message datagridview contents in body , set recipients. var client = new smtpclient("smtp.gmail.com", 587); client.enablessl = true; client.credentials = new networkcredential("jpbritopoker@gmail.com", "*****"); var mail = new mailmessage(); mail.from = new mailaddress("youraccount@yahoo.com"); mail.to.add("jpbritopoker@gmail.com"); mail.subject = "this subject of mail"; mail.body = "here want datagridview1"; client.send(mail); }
datagridview
special tool of dot net , email (which gets open in browser) doesn't know it.
besides, mail.body
accepts string value :). cannot assign entire datagridview i.e.
mail.body = datagridview1.tostring(); //wrong, utter useless
however, browsers know html. lets 1 thing, lets create equivalent html table of datagridview
so should iterate through each of rows of gridview , create html string , assign mail.body
.
do below in method:
private void btnsend_click_1(object sender, eventargs e) { string mailbody = "<table width='100%' style='border:solid 1px black;'>"; foreach (datagridviewrow row in datagridview1.rows) { mailbody +="<tr>"; foreach (datagridviewcell cell in row.cells) { mailbody +="<td stlye='color:blue;'>" +cell.value + "</td>"; } mailbody +="</tr>"; } mailbody +="</table>"; //your rest of original code mail.body = mailbody; client.send(mail); }
Comments
Post a Comment