Introduction
This article is provide a basic idea how we can send Emails from a C# Windows Application.
Here I have described how we can send mail with Attachment and HTML body.
Description
If we want to send mail then we need to know following things
1. SMTP Server address
2. Port No
Here I have used gmail as a SMTP Server
for gmail account
SMTP Server: smtp.gmail.com
SMTP port : 465 or 587
your account Name:###
your password:###
Lets Start Our Work
Step 1.
Add following Namespace
- using System.Net.Mail; //Added for using MailMessage class
- using System.Net; //Added for using NetworkCredential class
Step2.
Create your smtp client
- SmtpClient client = new SmtpClient();
- client.Host = txtServer.Text; //Set your smtp host address
- client.Port = int.Parse(txtPort.Text); // Set your smtp port address
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.Credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text); //account name and password
- client.EnableSsl = true; // Set SSL = true
Setup e-mail message
- MailMessage message = new MailMessage();
- message.To.Add(txtTo.Text); // Add Receiver mail Address
- message.From = new MailAddress(txtUsername.Text); // Sender address
- message.Subject = txtSubject.Text;
- message.IsBodyHtml = true; //HTML email
- message.Body = txtBody.Text;
Attaching File to message
- if (lstAttachedFiels.Items.Count > 0)
- {
- Attachment a;
- foreach ( string s in lstAttachedFiels.Items)
- {
- a = new Attachment(s);
- message.Attachments.Add(a); //Adding attachment to message
- }
- }
- client.Send(message);

Osama AhmedPosted Sep 3, 2021, 9:02 AM
With having ishtml = true and ssl = true, i receive the email but the format is not html, its like i'm receiving the email with html elements as it is.
Saber ShaikhPosted Sep 3, 2013, 1:32 PM
Thank you
Prashant BansalPosted Jan 4, 2011, 1:35 AM
Very nice Its working.