Richard
Richard

Reputation: 15

How to send picture to email body in WinForms?

I have a C# Winforms application I'm working on in Visual Studio 2010, the page in question is a bug reporting form - I have all the details set, email sends fine etc. My issue is attaching a screenshot to the email in the body, I have code set up to allow users to find and select a screenshot they take and attach to the form, but in the body itself it just gives me the text "System.Windows.Forms.Picturebox", or various similarities to that if I try .image.

I've had a look around via Google and here, but can only find topics that are to do with embedding the image or attaching it (and thus require to enter in a specific folder/image etc), whereas my users will be attaching their own image and name from different places. Is there anyway to get the image to be picked up without having to hardcode a location and name that my users will have to follow each time?

Code below:

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

Upvotes: 0

Views: 1519

Answers (1)

S.L.
S.L.

Reputation: 1076

Take a look to the following link

system.net.mail.mailmessage.attachments

You cannot put a winforms control inside the mailmessage :) it is outputted with ToString()... that is what you see in mail

Example

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

Take a look at your resources and dispose the memorystream. I didnt for the example, because i wrote it here in editor

Upvotes: 2

Related Questions