darklup darklup - 3 years ago 106
C# Question

Send mail with Embedded image win form c#

Hello what im trying to do is create email app where user can send email to other user and could embed the image in the mail (I don't want attachments).
So i have a code which lets me to do so, BUT i this code makes me to store pictures in specific location, where can i write in my own location from where the picture should be taken?

private void button2_Click(object sender, EventArgs e)
{
string htmlBody = "<html><body><h1>Picture</h1><br><a href=\"www.google.com\"><img src=\"cid:Pic1\"></a></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
// Create a LinkedResource object for each embedded image
LinkedResource pic1 = new LinkedResource("arrow.png", MediaTypeNames.Image.Jpeg);
pic1.ContentId = "Pic1";
avHtml.LinkedResources.Add(pic1);
// Add the alternate views instead of using MailMessage.Body
MailMessage m = new MailMessage();
m.AlternateViews.Add(avHtml);
// Address and send the message
m.From = new MailAddress("mymail@gmail.com", "Sender Name");
m.To.Add(new MailAddress("receivername@gmail.com", "Testing Subject"));
m.Subject = "A picture using alternate views";
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "mypassword");
client.EnableSsl = true;
client.Send(m);
}

Answer Source

I guess you can store your pictures wherever you want. Simply modify your line:

LinkedResource pic1 = new LinkedResource("arrow.png", MediaTypeNames.Image.Jpeg);

... including the complete path. For example, if you got the path stored in a variable called PICTURES_PATH, you can do:

LinkedResource pic1 = new LinkedResource(System.IO.Path.Combine(PICTURES_PATH, "arrow.png"), MediaTypeNames.Image.Jpeg);

Hope that helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download