我无法将html邮件中的gridview内容发送为html. [英] Im unable to send the content of gridview in the mail body as html.

查看:111
本文介绍了我无法将html邮件中的gridview内容发送为html.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请在下面找到代码.

我曾经因为"对象引用未设置为对象实例"而收到错误消息.

代码如下:

Hi,

Please find the code below .

I used to get an error as "object reference not set to an instance of object".

code is as below:

private void button2_Click(object sender, EventArgs e)
{

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

try
{

mailItem.Subject = "This is the subject";
mailItem.To = "someexample@";
mailItem.CC = "someexample@;some@";
mailItem.Body = "This is the message.";
mailItem.BodyFormat=Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;

mailItem.Body = htmlMessageBody(dataGridView1).ToString();

mailItem.Display(false);

}
catch (Exception ex)
{
MessageBox.Show("Error! " + ex.Message);
}
}
private StringBuilder htmlMessageBody(DataGridView dg)
{
StringBuilder strB = new StringBuilder();

strB.AppendLine("<html><body>
");
strB.AppendLine("");

for (int i = 0; i < dg.Columns.Count; i++)
{
strB.AppendLine("");
}

strB.AppendLine("");
for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine("");
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
strB.AppendLine("");
}
strB.AppendLine("");

}

strB.AppendLine("
" + dg.Columns[i].HeaderText + "
" + dgvc.Value.ToString() + "
</body></html>");
return strB;
} 

推荐答案

执行以下步骤

1.编写查询并从数据库中检索数据.
2.现在创建一个html表.
3.在电子邮件中发送带有相关数据的html表.

希望以上三个步骤能解决您的问题.
Do the following steps

1. Write a query and retrieve the data from the database.
2. Now create a html table.
3. Send the html table with the relevant data in the email.

Hope the above three steps solve your problem.


这篇关于我无法将html邮件中的gridview内容发送为html.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆