Friday, May 22, 2009

How To: Sending Email Using C#

in this article I'll show you how can you send mail using windows application
I'm using gmail becouse it allowed sending and recieving Emails and you need to make some configurations like:
Host Name:
Port Number: 587
Your Email Address:
your Password: your Email Password
SSL:true (becouse gmail using ssl)

Let's start...

Create Windows Application using C# :
Create Form Like the Below Form with Controls

Important Note Controls Names Will be Like:

textbox =txt...

using System.Net;
using System.Net.Mail;

then create some properities :
you can put them in seperated class but I'm putted them in the same form because it's a sample

public string MailFrom { get; set; }
public string MailTo { get; set; }
public string MailBody { get; set; }
public string MailSubject { get; set; }
public string AttachmentFile { get; set; }
public string HostName { get; set; }
public int PortNumber { get; set; }
public string EmailAddress { get; set; }
public string EmailPassword { get; set; }
public bool SSL { get; set; }

then create method for sending mail:

public void SendingMail()
MailMessage mailMessage = new MailMessage(MailFrom,MailTo,MailSubject,MailBody);
Attachment fileAttachment = new Attachment(AttachmentFile);
SmtpClient smtpClient = new SmtpClient(HostName,PortNumber);
smtpClient.Credentials = new NetworkCredential(EmailAddress,EmailPassword);
smtpClient.EnableSsl = SSL;

in attachment button put the following code:

private void btnAttachment_Click(object sender, EventArgs e)
OpenFileDialog fileDialog = new OpenFileDialog();
lblAttachment.Text = fileDialog.FileName;

in Send Mail Button put the following code:

private void btnSendMail_Click(object sender, EventArgs e)
MailFrom = txtMailAddress.Text; ;
MailTo = txtTo.Text;
MailBody = txtBody.Text;
MailSubject = txtSubject.Text;
AttachmentFile = lblAttachment.Text;
HostName = txtHostName.Text;
PortNumber = Convert.ToInt16(txtPortNo.Text);
EmailAddress = txtMailAddress.Text;
EmailPassword = txtMailPassword.Text;
SSL = chkSSL.Checked;
MessageBox.Show("Sending Mail Succeeded");
catch (Exception ex)


  1. That is perfect, but I think it needs a higher security level isn't it?

  2. first thing that's a sample application, and it's a windows application not a web application
    so what's the best way to store password ?!!!

    in windows application we can store it in config file and store it encrypted.

    but in web application we can store it in web.config or in cookie and also encrypted

    and when we will use the password we will use a class to decrypt it.

    but my articles are sample code or sample application.

    Waleed Elkot

  3. Yesterday I entered my MS Outlook and couldn't receive new emails, because of my password had been crashed on undetermined cause. Luckily for later I by accident got - microsoft outlook get password on the Inet. The tool saved me. It recovered my data for seconds and without my money.

  4. it's show a error in my application.
    "The SMTP server requrires a secure connection or the client was not authenticated. The server response was: 5.5.1 authentication required, learn more at."

    how can I solve this problem please suggest.