How to Send an Email With Image in C#

Introduction

This article describes how to send an email with image in C#. For sending the email, you need to configure the email services on server.

Note: For more info about configuration read how to configure mail server (http://technet.microsoft.com/en-us/library/cc780996%28v=ws.10%29.aspx).

To explain this article, I will use the following procedure after configuring the server:

  • Create a new website and add a page into it.
  • Add 3 textboxes into the page for receiver Email-ID, subject of Email and message of Email and a button for sending mail.
  • Write the some code on ".cs" file to send the mail with some Text and an Image at a "button click" event.

The following procedure is the details of the preceding steps.

Step 1

Create a new empty Website named "MailServer".



Step 2

  • Add a new Page named "SendingMail.aspx".



  • Add a Button with Onclick event (for sending the email) on the page.
    1. <table>  
    2.             <tr>  
    3.                 <td>Mail To:-</td>  
    4.                 <td>  
    5.                     <asp:TextBox ID="txtEmail" runat="server" Width="200px">  
    6.                     </asp:TextBox></td>  
    7.             </tr>  
    8.             <tr>  
    9.                 <td>Subject:-</td>  
    10.                 <td>  
    11.                     <asp:TextBox ID="txtSubject" runat="server" Width="200px">  
    12.                     </asp:TextBox></td>  
    13.             </tr>  
    14.             <tr>  
    15.                 <td>Message:-</td>  
    16.                 <td>  
    17.                     <asp:TextBox ID="txtmessagebody" runat="server" TextMode="MultiLine" Height="200px" Width="400px">  
    18.                     </asp:TextBox></td>  
    19.             </tr>  
    20.             <tr>  
    21.                 <td colspan="2" align="center">  
    22.                     <asp:Button ID="btn_send" runat="server" Text="Send Mail"  
    23.                         OnClick="btn_send_Click" /></td>  
    24.             </tr>  
    25.         </table>  


  • Add an image named "photo.jpg" into the folder named "Images".



  • Add the 4 namespaces at the top of the ".cs" file.


    1. using System.Net.Mail;  
    2. using System.IO;  
    3. using System.Text;  
    4. using System.Net.Mime;  
  • Write the following code to send the email on the click event of the button.
    1.    protected void btn_send_Click(object sender, EventArgs e)  
    2.     {  
    3.         try  
    4.         {  
    5.             MailMessage message = new MailMessage();  
    6.             message.To.Add(txtEmail.Text);// Email-ID of Receiver  
    7.             message.Subject = txtSubject.Text;// Subject of Email  
    8.             message.From = new   
    9. System.Net.Mail.MailAddress("[email protected]");// Email-ID of Sender  
    10.             message.IsBodyHtml = true;  
    11.             message.AlternateViews.Add(Mail_Body());  
    12.             SmtpClient SmtpMail = new SmtpClient();  
    13.             SmtpMail.Host = "Your Host";//name or IP-Address of Host used for SMTP transactions  
    14.             SmtpMail.Port = 25;//Port for sending the mail  
    15.             SmtpMail.Credentials = new   
    16. System.Net.NetworkCredential("""");//username/password of network, if apply  
    17.             SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;  
    18.             SmtpMail.EnableSsl = false;  
    19.             SmtpMail.ServicePoint.MaxIdleTime = 0;  
    20.             SmtpMail.ServicePoint.SetTcpKeepAlive(true, 2000, 2000);  
    21.             message.BodyEncoding = Encoding.Default;  
    22.             message.Priority = MailPriority.High;  
    23.             SmtpMail.Send(message); //Smtpclient to send the mail message  
    24.             Response.Write("Email has been sent");  
    25.         }  
    26.         catch (Exception ex)  
    27.         { Response.Write("Failed"); }  
    28.     }  

Note: the "SmtpMail.Host" value will be your hosting name or IP Address and the "SmtpMail.Port" will also vary.


  1. private AlternateView Mail_Body()  
  2.     {  
  3.         string path = Server.MapPath(@"Images/photo.jpg");  
  4.         LinkedResource Img = new LinkedResource(path, MediaTypeNames.Image.Jpeg);  
  5.         Img.ContentId = "MyImage";  
  6.         string str = @"  
  7.             <table>  
  8.                 <tr>  
  9.                     <td> '" + txtmessagebody.Text + @"'  
  10.                     </td>  
  11.                 </tr>  
  12.                 <tr>  
  13.                     <td>  
  14.                       <img src=cid:MyImage  id='img' alt='' width='100px' height='100px'/>   
  15.                     </td>  
  16.                 </tr></table>  
  17.             ";  
  18.         AlternateView AV =   
  19.         AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);  
  20.         AV.LinkedResources.Add(Img);  
  21.         return AV;  
  22.     }  



Step 3 
  • Run the page on server that will be like:



  • After filling in the valid Email ID, Subject of Email and message of Email, click on the "Send mail" button to send the email.


Result: Now you can see that, I (the receiver) got the email.



Up Next
    Ebook Download
    View all
    Learn
    View all