如何在WinForms中发送图片到邮件正文
本文关键字:正文 WinForms | 更新日期: 2023-09-27 18:18:04
我有一个c# Winforms应用程序,我正在Visual Studio 2010中工作,有问题的页面是一个错误报告表单-我有所有的详细信息设置,电子邮件发送良好等。我的问题是在正文中附加一个截图到电子邮件,我已经设置了代码,允许用户找到并选择他们所采取的截图并附加到表单,但在正文本身,它只是给了我一个文本"System.Windows.Forms。如果我尝试。image.
我已经通过谷歌和这里看了一下,但只能找到与嵌入图像或附加它有关的主题(因此需要在特定文件夹/图像等中输入),而我的用户将从不同的地方附加他们自己的图像和名称。有没有办法让图像被捡起来,而不必硬编码的位置和名称,我的用户将不得不遵循每次?
下面的代码: private void btnBugEmail_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try
{
SmtpClient client = new SmtpClient("details here");
MailMessage message = new MailMessage();
message.From = new MailAddress("email here");
string mailBox = txtBugAdd.Text.Trim();
message.To.Add(mailBox);
string mailFrom = txtEmailFromBug.Text.Trim();
message.CC.Add(mailFrom);
string mailCC = txtMailCCBug.Text.Trim();
message.Bcc.Add(mailCC);
message.IsBodyHtml = true;
message.Body = "Bug Report - please see below: " +
"'n" + "<br>" + "<b>" + "1. What were you doing at the time of the error?" + "</b>" +
"'n" + "<br>" + rtbTimeOfError.Text +
"'n" + "<br>" + "<b>" + "2. Are you able to repeat the steps and achieve the same error?" + "</b>" +
"'n" + "<br>" + rtbCanRepeat.Text +
"'n" + "<br>" + "<b>" + "3. Does this problem happen again if you change any of the details you have entered?" + "</b>" +
"'n" + "<br>" + rtbChangeDetails.Text;
message.Subject = "Bug Report";
var image = pboxBugImage.Image;
using(var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
message.Attachments.Add(new Attachment(ms, "Screenshot.jpg"));
client.Credentials = new System.Net.NetworkCredential("credentials here");
client.Port = System.Convert.ToInt32(25);
client.Send(message);
}
new Endpage().Show();
this.Close();
}
catch
{
MessageBox.Show("my comment here");
}
}
请看下面的链接
system.net.mail.mailmessage.attachments
你不能把winforms控件放在mailmessage里面:)它是用ToString()
输出的…这就是你在邮件中看到的
private void btnBugEmail_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try
{
SmtpClient client = new SmtpClient("details here");
MailMessage message = new MailMessage();
message.From = new MailAddress("email here");
string mailBox = txtBugAdd.Text.Trim();
message.To.Add(mailBox);
string mailFrom = txtEmailFromBug.Text.Trim();
message.CC.Add(mailFrom);
string mailCC = txtMailCCBug.Text.Trim();
message.Bcc.Add(mailCC);
message.IsBodyHtml = true;
message.Body = "Bug Report - please see below: " +
"'n" + "<br>" + "<b>" + "1. What were you doing at the time of the error?" + "</b>" +
"'n" + "<br>" + rtbTimeOfError.Text +
"'n" + "<br>" + "<b>" + "2. Are you able to repeat the steps and achieve the same error?" + "</b>" +
"'n" + "<br>" + rtbCanRepeat.Text +
"'n" + "<br>" + "<b>" + "3. Does this problem happen again if you change any of the details you have entered?" + "</b>" +
"'n" + "<br>" + rtbChangeDetails.Text;
message.Subject = "Bug Report";
var image = pboxBugImage.Image;
using(var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
message.Attachments.Add(new Attachment(ms, "Screenshot.jpg"));
client.Credentials = new System.Net.NetworkCredential("credentials here");
client.Port = System.Convert.ToInt32(25);
client.Send(message);
}
new Endpage().Show();
this.Close();
}
catch
{
MessageBox.Show("my comment here");
}
}
查看您的资源并处理内存流。这个例子我没有写,因为我写在编辑器