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".

Step 2
- Add a new Page named "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.

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;
}

Step 3
- Run the page on the 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.


Tim MackeyPosted Dec 12, 2024, 6:24 PM
Chrome browser removes the image as a security feature.
Hung rockPosted Nov 15, 2021, 1:33 PM
Super, you save my day
Alfred FotiPosted Feb 11, 2021, 1:42 AM
I need to send a email to a dedicated email address. It should include a dedicated Subject lie and body txt
Bhavesh JadavPosted Aug 27, 2018, 3:30 AM
Nice.......................
Lakshmi NaiduPosted Jul 12, 2018, 9:09 AM
This is very useful to me to fixed the issue today. Thanks
Mitesh TemrePosted Jun 8, 2018, 3:22 AM
Hello did this things works fine in outlook 2016 ?