如何在WinForms中将图片发送到电子邮件正文?

时间:2014-08-15 11:29:45

标签: c# winforms

我有一个C#Winforms应用程序我在Visual Studio 2010中工作,有问题的页面是一个错误报告表单 - 我已经设置了所有详细信息,电子邮件发送正常等等。我的问题是将屏幕截图附加到在正文中的电子邮件中,我设置了代码,允许用户查找并选择他们拍摄的屏幕截图并附加到表单,但在正文本身中,它只是给了我文本" System.Windows.Forms.Picturebox& #34;,或者如果我尝试.image的各种相似之处。

我已经通过Google浏览了这里,但只能查找与嵌入图片或附加图片有关的主题(因此需要输入特定的文件夹/图像等),而我的用户将从不同的地方附加自己的图像和名称。无论如何都要获取图像而不必硬编码我的用户每次都必须遵循的位置和名称?

以下代码:

    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");
    }
}

1 个答案:

答案 0 :(得分:2)

查看以下链接

system.net.mail.mailmessage.attachments

你不能把winform控件放在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");
    }
}

查看您的资源并处理内存流。我没有这个例子,因为我在编辑器中写了这个