gokul
gokul

Reputation: 1

how to attach image in picture box in mail in c# windows form

I am trying to upload an image to a database and send an email when a user clicks a Save button. The database upload works when the user clicks save, but the email is not sending correctly.

Have I formatted msg.body correctly?

try
{
    u.Open();
    SqlCommand i = new SqlCommand("insert into BMS values('" + p + "')", u);
    i.ExecuteNonQuery();

    MailMessage Msg = new MailMessage();

    Msg.From = new MailAddress("[email protected]", "*****");
    Msg.To.Add("[email protected]");

    string _fname = pictureBox1.ToString();

    Msg.Attachments.Add(new Attachment(_fname));
    Msg.Subject = "user credential sent from bank ";
    Msg.Body = "<img src=@'+ pictureBox.Image + />";                
    Msg.IsBodyHtml = true;

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "****");
    smtp.EnableSsl = true;
    smtp.Send(Msg);

    MessageBox.Show("Data inserted successfully and data's mailed ");

    u.Close();
}
catch (Exception ex)
{
    Console.WriteLine("{0} Exception caught.", ex);
}

Upvotes: 0

Views: 1116

Answers (1)

Rufat Asgarov
Rufat Asgarov

Reputation: 11

If you want to show image in email body, you need upload image any website and put in email like that

Msg.Body = "<img src=" + pic_url + " />";

Try below code for send image , also i suggest you add values with paramaters in sqlcommand

 SqlConnection con = new SqlConnection("connection string");
        SqlCommand com = new SqlCommand("insert into BMS values(@value1)", con);
        com.Parameters.AddWithValue("@value1", p);
        try
        {
            com.Connection.Open();
            com.ExecuteNonQuery();

            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("your email", "your display name");
            Msg.To.Add("to user name");
            string _fname = pictureBox1.ToString();

            Image image = pictureBox1.Image;

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            image.Save(stream, ImageFormat.Jpeg);
            stream.Position = 0;

            Msg.Attachments.Add(new Attachment(stream, "Screenshot.jpg"));

            Msg.Subject = "user credential sent from bank ";


            Msg.Body = "content here ";
            Msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "mail.cidcode.net";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("your email", "your password");
            smtp.Send(Msg);

            MessageBox.Show("Data inserted successfully and data's mailed ");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Test " + ex.Message);
            Console.WriteLine("{0} Exception caught.", ex);
        }
        finally
        {
            com.Connection.Close();
            com.Dispose();
        }

Upvotes: 1

Related Questions