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;
- using System.Net;
Step2.
Create your smtp client
- SmtpClient client = new SmtpClient();
- client.Host = txtServer.Text;
- client.Port = int.Parse(txtPort.Text);
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.Credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text);
- client.EnableSsl = true;
Step 3.
Setup e-mail message
- MailMessage message = new MailMessage();
- message.To.Add(txtTo.Text);
- message.From = new MailAddress(txtUsername.Text);
- message.Subject = txtSubject.Text;
-
- message.IsBodyHtml = true;
- 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);
- }
- }
Sending Mail