datagridview to html table and send email

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
// binding data from xml file to datagridview
DataSet ds = new DataSet();
ds.ReadXml(
" dataFile.xml " ); // xml file placed in bin/Debug folder
dataGridView1.DataSource = ds;
dataGridView1.DataMember
= " authors " ;

private StringBuilder htmlMessageBody(DataGridView dg)
{
StringBuilder strB
= new StringBuilder();
// create html & table
strB.AppendLine( " <html><body><center>< " +
" table border='1' cellpadding='0' cellspacing='0'> " );
strB.AppendLine(
" <tr> " );
// cteate table header
for ( int i = 0 ; i < dg.Columns.Count; i ++ )
{
strB.AppendLine(
" <td align='center' valign='middle'> " +
dg.Columns[i].HeaderText
+ " </td> " );
}
// create table body
strB.AppendLine( " <tr> " );
for ( int i = 0 ; i < dg.Rows.Count; i ++ )
{
strB.AppendLine(
" <tr> " );
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
strB.AppendLine(
" <td align='center' valign='middle'> " +
dgvc.Value.ToString()
+ " </td> " );
}
strB.AppendLine(
" </tr> " );

}
// table footer & end of html file
strB.AppendLine( " </table></center></body></html> " );
return strB;}

// Create a message with datagridview contents
// in its body and set up the recipients.
MailMessage myMessage = new MailMessage();
try
{
myMessage.From
= " from@yourDomain.com " ; // place here from address
myMessage.To = " to@recipientsDomain " ; // place here to address
myMessage.Cc = " cc@someDomain.com " ; // place here copy address
myMessage.BodyEncoding = Encoding.UTF8;
myMessage.BodyFormat
= MailFormat.Html;
// call method, creating HTML from datagridview
myMessage.Body = htmlMessageBody(dataGridView1).ToString();
// place here your subject
myMessage.Subject = " message subject " ;

// if it is needed set up credentials
myMessage.Fields.Add( " http://schemas.microsoft.com/cdo/ " +
" configuration/smtpauthenticate " ,
" 1 " ); // Basic Authentication
myMessage.Fields.Add( " http://schemas.microsoft.com/cdo/ " +
" configuration/sendusername " ,
" userName " ); // user name
myMessage.Fields.Add( " http://schemas.microsoft.com/cdo/ " +
" configuration/sendpassword " ,
" password " ); // password
// place here SMTP server
SmtpMail.SmtpServer = " your SMTP server " ;
SmtpMail.Send(myMessage);
MessageBox.Show(
" Message sent! " );
}
catch (Exception ex)
{
MessageBox.Show(
" Error! " + ex.Message);
}

// create message with datagridview contents in attachment
MailMessage myMessage = new MailMessage();
try
{
myMessage.From
= " from@yourDomain.com " ; // place here from address
myMessage.To = " to@recipientsDomain " ; // place here to address
myMessage.Cc = " cc@someDomain.com " ; // place here copy address
myMessage.BodyEncoding = Encoding.UTF8;
myMessage.BodyFormat
= MailFormat.Html;
myMessage.Body
= " some body text " ; // place here some text
myMessage.Subject = " message subject " ; // place here your subject
// creating attachment
StreamWriter sw = new StreamWriter( " file.html " ,
true , Encoding.UTF8); // creating html file
// write datagridview contents to HTML file
sw.Write(htmlMessageBody(dataGridView1).ToString());
sw.Close();
// create attachment
MailAttachment myAttach = new MailAttachment( " file.html " );
myMessage.Attachments.Add(myAttach);
// add attachment to our message

// if it is needed set up credentials
myMessage.Fields.Add( " http://schemas.microsoft.com/cdo/ " +
" configuration/smtpauthenticate " ,
" 1 " ); // Basic Authentication
myMessage.Fields.Add( " http://schemas.microsoft.com/cdo/ " +
" configuration/sendusername " ,
" userName " ); // user name
myMessage.Fields.Add( " http://schemas.microsoft.com/cdo/ " +
" configuration/sendpassword " ,
" password " ); // password
// place here SMTP server
SmtpMail.SmtpServer = " your SMTP server " ;
SmtpMail.Send(myMessage);
MessageBox.Show(
" Message sent! " );
}
catch (Exception ex)
{
MessageBox.Show(
" Error! " + ex.Message);
}

 

转载于:https://www.cnblogs.com/zzandww/archive/2010/05/06/1729087.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值