【发布时间】:2020-12-05 18:37:55
【问题描述】:
下面的函数负责获取面板的屏幕截图并将其用作电子邮件中的附件,而不是将其存储在本地机器中。
我设法做到了,但有一个小问题,它给了我这个错误:
无法从“System.Drawing.Bitmap”转换为“字符串”
我知道有类似的问题,但我不知道如何解决这个问题。
private void btnSend_Click(object sender, EventArgs e)
{
//using (var bmp = new Bitmap(workPanel.Width, workPanel.Height))
//{
// workPanel.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
// bmp.Save(@"images/" + name.Text + ".png");
//}
try
{
using (var bmp = new Bitmap(workPanel.Width, workPanel.Height))
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("email@gmail.com");
mail.To.Add(txtTo.Text); // Receivers email address comes here
mail.Subject = txtTitle.Text;
mail.Body = txtBody.Text;
mail.IsBodyHtml = true; // TO HAVE THE BODY AS HTML
System.Net.Mail.Attachment attachment;
//attachment = new System.Net.Mail.Attachment(lblLocation.Text);
attachment = new System.Net.Mail.Attachment(bmp); // <-------- ERROR COMES HERE
mail.Attachments.Add(attachment);
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
MessageBox.Show("Mail has been sent");
}
}
catch(Exception ex)
{
//MessageBox.Show(ex.Message);
MessageBox.Show("Something went wrong, this might be a problem from our end.\n" + ex.Message, "Please contact support team");
}
}
【问题讨论】:
-
在来这里之前总是尝试 MSDN.. - 除了字符串之外,您需要在附件中包含 MediaType constructor:mediatypenames
-
我的情况是什么
-
这能回答你的问题吗? C# Attaching System.Drawing.Image to Email
-
很遗憾没有:(
-
Guru 的链接包含答案。 docs.microsoft.com/pt-br/dotnet/api/…。没有直接接受 Bitmap 对象的构造函数。