How to Send an Email With Image in C#

Introduction

This article describes how to send an email with an image in C#. For sending the email, you need to configure the email services on the 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 to 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 some code on the ".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".

MailServer

Step 2

  • Add a new Page named "SendingMail.aspx".
    SendingMail.aspx
  • Add a Button with the Onclick event (for sending the email) on the page.
    <table>
        <tr>
            <td>Mail To:-</td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server" Width="200px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Subject:-</td>
            <td>
                <asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Message:-</td>
            <td>
                <asp:TextBox ID="txtmessagebody" runat="server" TextMode="MultiLine" Height="200px" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Button ID="btn_send" runat="server" Text="Send Mail" OnClick="btn_send_Click" />
            </td>
        </tr>
    </table>
    
  • Add an image named "photo.jpg" into the folder named "Images".
  • Add the 4 namespaces at the top of the ".cs" file.
    using System.Net.Mail;
    using System.IO;
    using System.Text;
    using System.Net.Mime;
    
  • Write the following code to send the email on the click event of the button.
    protected void btn_send_Click(object sender, EventArgs e)   
    {   
        try   
        {   
            MailMessage message = new MailMessage();   
            message.To.Add(txtEmail.Text);// Email-ID of Receiver   
            message.Subject = txtSubject.Text;// Subject of Email   
            message.From = new System.Net.Mail.MailAddress("[email protected]");// Email-ID of Sender   
            message.IsBodyHtml = true;   
            message.AlternateViews.Add(Mail_Body());   
            SmtpClient SmtpMail = new SmtpClient();   
            SmtpMail.Host = "Your Host";//name or IP-Address of Host used for SMTP transactions   
            SmtpMail.Port = 25;//Port for sending the mail   
            SmtpMail.Credentials = new System.Net.NetworkCredential("", "");//username/password of network, if apply   
            SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;   
            SmtpMail.EnableSsl = false;   
            SmtpMail.ServicePoint.MaxIdleTime = 0;   
            SmtpMail.ServicePoint.SetTcpKeepAlive(true, 2000, 2000);   
            message.BodyEncoding = Encoding.Default;   
            message.Priority = MailPriority.High;   
            SmtpMail.Send(message); //Smtpclient to send the mail message   
            Response.Write("Email has been sent");   
        }   
        catch (Exception ex)   
        { Response.Write("Failed"); }   
    }   
    

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

SmtpMail.Host

private AlternateView Mail_Body()
{
    string path = Server.MapPath(@"Images/photo.jpg");
    LinkedResource Img = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
    Img.ContentId = "MyImage";
    string str = @"
        <table>
            <tr>
                <td> '" + txtmessagebody.Text + @"' 
                </td>
            </tr>
            <tr>
                <td>
                    <img src=cid:MyImage  id='img' alt='' width='100px' height='100px'/>   
                </td>
            </tr></table>
            ";
    AlternateView AV =   
    AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);
    AV.LinkedResources.Add(Img);
    return AV;
}

Mail

Step 3

  • Run the page on the server that will be like.
    Server
  • After filling in the valid Email ID, Subject of Email, and message of Email, click on the "Send mail" button to send the email.
    Send mail

Result

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

Receiver