The function below is responsible for getting the screenshot of a panel and using it as an attachment in an email without storing it in the local machine.
I managed to do it but one small problem, it gives me this error:
cannot convert from 'System.Drawing.Bitmap' to 'string'
I know there are similar questions to this but I cant figure out how i can fix this.
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");
}
}