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.
- <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);
- message.Subject = txtSubject.Text;
- message.From = new
- System.Net.Mail.MailAddress("[email protected]");
- message.IsBodyHtml = true;
- message.AlternateViews.Add(Mail_Body());
- SmtpClient SmtpMail = new SmtpClient();
- SmtpMail.Host = "Your Host";
- SmtpMail.Port = 25;
- SmtpMail.Credentials = new
- System.Net.NetworkCredential("", "");
- 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);
- 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 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.