Sending Emails Through C#

29 Mar

Here’s how you can develop your own Email sender in C# Windows Application with the inbuilt SMTP protocol using System.Net.Mail

3 Prerequisites:
  • Search for your Email Service Provider’s SMTP Server Address.
  • The Server Port Number
  • Configure your account settings to enable POP service so that the application is able to send mails via your account.

(POP is for outgoing mails and IMAP is for incoming mails. Since we are going to create an email sender and not an email receiver we need only POP enabled.)

Here we wil take the example of GMAIL,
  • Server Address:
  • Server Port: 587
  • For account configuration login to Gmail, goto Settings, goto Forwarding and POP/IMAP, and Check on “Enable POP for all mail” and save the settings.

1. Now create a new windows application
2. Drag and drop the following controls from the toolbar:
  • 7 Text Boxes
  • 1 Rich Text Box
  • 2 Buttons
  • 8 Labels

3. Text boxes are renamed as:
  • textBoxId
  • textBoxPassword
  • textBoxSendTo
  • textBoxSubject
  • textBoxAttachment
  • textBoxAddr
  • textBoxPort

4. Buttons are renamed as:
  • buttonBrowse
  • buttonSend

5. Rich Text Box is renamed as richTextBoxMessage.
6. Provide a Password Char “*” (or any other) for the textBoxPassword.

7. Move the controls to give your form the following look:

8. Include the following line on the top of the .cs file:
using System.Net.Mail

9. At the buttonBrowse click event place the following code:
textBoxAttachment.Text = openFileDialog1.FileName;

This will open up file browser dialogue for selecting an attachment, and the location of selected file is put into the textbox.

10. At the buttonSend click event place the following code:

SmtpClient client = new SmtpClient(textBoxAddr.Text);
MailMessage message = new MailMessage(textBoxId.Text, textBoxSendTo.Text);
message.Body = richTextBoxMessage.Text;
message.Subject = textBoxSubject.Text;
if (textBoxAttachment.Text != “”)
{ message.Attachments.Add(new Attachment(textBoxAttachment.Text)); }
client.Credentials = new System.Net.NetworkCredential(textBoxId.Text, textBoxPassword.Text);
client.Port = Convert.ToInt32(textBoxPort.Text);
MessageBox.Show(“Message delivered successfully!!!”, “ashKAP Mail Sender”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
catch (Exception ex)
MessageBox.Show(“Message could not be sent.\nThe following error was returned:\n'” + ex.Message + “‘”, “ashKAP Mail Sender”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

Run the project and start mailing!!!!!!!!
Download the ashKAP Mail Sender Project from the Box on the right side.
(The project is made in Visual Studio 2010 and will not open in lower versions)

Here’s the software I made “ashKAP Prism”

Leave a comment

Posted by on March 29, 2011 in C#


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: