[英]An easy way to send data in datagrid to an email address?
我想将数据网格中的数据发送到电子邮件,因此我将数据网格的名称放在邮件正文区域中,但它显示它无法将其转换为字符串数据。
这是我的代码 - 请帮助我:
private void emailgrid(object sender, RoutedEventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = DataGrid;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
您不能像尝试那样简单地“通过电子邮件发送DataGrid
”。 电子邮件显示文本,有时显示图片 - 不是完整的 WPF 控件。 您收到错误消息的原因是MailMessage.Body
是一个string
属性,并且string
和DataGrid
之间没有内置转换。
您需要做的是将DataGrid
的内容转换为可在电子邮件中显示的格式。
如果将MailMessage.IsBodyHtml
设置为true
,则可以将 HTML 内容放入Body
属性中。 这允许您使用诸如table
类的 HTML 元素,这将使您正确地正确显示DataGrid
的内容。 您应该能够在网上找到足够的信息来根据您的数据构建一个 HTML 表格,或者甚至可以找到已经发布代码的其他人已经这样做了。
这是问题的答案
private void emailgrid(object sender, RoutedEventArgs e)
{
try
{
MailMessage mail = new MailMessage();
//put your SMTP address and port here.
SmtpClient SmtpServer = new SmtpClient("");
//Put the email address
mail.From = new MailAddress("email@gmail.com");
//Put the email where you want to send.
mail.To.Add("email@gmail.com");
mail.Subject = "suubject";
StringBuilder sbBody = new StringBuilder();
sbBody.AppendLine("body");
mail.Body = sbBody.ToString();
//Your log file path
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(@"C:\XML\see.xml");
mail.Attachments.Add(attachment);
//Your username and password!
SmtpServer.Credentials = new System.Net.NetworkCredential("UserName", "Password");
//Set Smtp Server port
SmtpServer.Port = 25;
//SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Hello User Your Mail Has Been Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.