There are a few different ways that you can send images via email. The easiest way to do this is to simply make the body of the email html and add in an <img> tag that links directly to the image on the internet. However, because this has been abused by email spammers an image sent like that will most likely not be viewed depending on the recipients email settings. In this tutorial we will demonstrate how to send an email with an attached image displayed in the messages body.
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud servers!
Adding the Default.aspx PageTo demonstrate this I am going to create a simple web site with a button in it that I will use to send the email. At this point in the tutorial I have created a new ASP.NET Empty Web Site. To begin:
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud servers!
Adding the Default.aspx PageTo demonstrate this I am going to create a simple web site with a button in it that I will use to send the email. At this point in the tutorial I have created a new ASP.NET Empty Web Site. To begin:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a button onto the web form.
- Right click the project in your solution explorer.
- Select add existing item...
- Select the image you want to add.
- Click add.
- Open Default.aspx to design mode.
- Double click the button we added earlier to generate the click event method for it.
- Add the following using statements at the top of the Default.aspx.cs class:
using System.Net.Mail; using System.Text; using System.Net;
ButtonClick _Event
{ //create the message MailMessage mail = new MailMessage(); //add the email address we will be sending the message to mail.To.Add("Recipient@gmail.com"); //add our email here mail.From = new MailAddress("Sender@Gmail.com"); //email's subject mail.Subject = "In line image test"; //email's body, this is going to be html. note that we attach the image as using cid mail.Body = "Hello email. <br/> <img src=\"cid:tmpImage.gif\">"; //set email's body to html mail.IsBodyHtml = true; //add our attachment Attachment imgAtt = new Attachment(Server.MapPath("tmpImage.gif")); //give it a content id that corresponds to the src we added in the body img tag imgAtt.ContentId = "tmpImage.gif"; //add the attachment to the email mail.Attachments.Add(imgAtt); //setup our smtp client, these are Gmail specific settings SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.EnableSsl = true; //ssl must be enabled for Gmail //our Gmail account credentials NetworkCredential credentials = new NetworkCredential("Sender@Gmail.com", "Password"); //add credentials to our smtp client client.Credentials = credentials; try { //try to send the mail message client.Send(mail); } catch { //some feedback if it does not work Button1.Text = "Fail"; } }
No comments:
Post a Comment