Click here to Skip to main content
11,488,877 members (75,384 online)
Click here to Skip to main content

Tagged as

Sending Email with Gmail, Yahoo, AOL, and Live Mail Via SMTP

, 27 May 2010 CPOL 68.4K 3.5K 91
Rate this:
Please Sign up or sign in to vote.
Simple Email with Gmail, Yahoo, AOL, and Live mail via SMTP

Introduction

My name is Charles Henington and I will be showing you how to send email with C# Winform via Gmail, Yahoo and Windows Live accounts. These will be sent using SMTP and will all use the same basic lines of code with slight variations.

Acknowledgements

This code is built based on a simple email app built by a good guy named Abhishek Sur. His article can be found here.

Getting Started

You will need 5 textboxes, 6 labels, 1 listbox, and 3 buttons. You will leave all items as default names example (textBox1, textBox2, etc.) change text on label1 to (TO). Change label2 text to (GMAIL ACCT), label3 to (Password), label4 to (Subject), label5 to (Message body). Label 1-5 will go with the appropriate text box 1-5. Now change button1 text to (Add Attachment), button2 to (Remove Attachment), and button3 to (Send Mail). Now take textBox5 and make it multiline and stretch it out until your text box resembles a text area. Now add a listBox this list box will handle your attachments and change label6 to (Attachments).

Using the Code

Using the code is very self explanatory if you have left the default names like I had mentioned earlier.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;

		namespace EmailClient
	{
		public partial class GoogleForm : Form
	{
		MailMessage mail = new MailMessage();
		public GoogleForm()
	{
		InitializeComponent();
	}            
		private void button1_Click(object sender, EventArgs e)
	{
		smtp_csharp.frmAddAttachment frm = new smtp_csharp.frmAddAttachment();
		frm.ShowDialog(this);
		if (frm.txtFile.Text.Trim() != "")
		listBox1.Items.Add(frm.txtFile.Text);
		frm.Dispose();
	}
		private void button2_Click(object sender, EventArgs e)
	{
		if (listBox1.SelectedIndex > -1)
		listBox1.Items.RemoveAt(listBox1.SelectedIndex);
	}
		private void button3_Click(object sender, EventArgs e)
	{
		using (MailMessage mailMessage = 
			new MailMessage(new MailAddress(textBox1.Text),
		new MailAddress(textBox1.Text)))
	{
		mailMessage.Body = textBox5.Text;
		mailMessage.Subject = textBox4.Text;
		try
	{
		SmtpClient SmtpServer = new SmtpClient();
		SmtpServer.Credentials = 
		    new System.Net.NetworkCredential(textBox2.Text, textBox3.Text);
		SmtpServer.Port = 587;
		SmtpServer.Host = "smtp.gmail.com";
		SmtpServer.EnableSsl = true;
		mail = new MailMessage();
		String[] addr = textBox1.Text.Split(',');
		mail.From = new MailAddress(textBox2.Text);
		Byte i;
		for (i = 0; i < addr.Length; i++)
		mail.To.Add(addr[i]);
		mail.Subject = textBox4.Text;
		mail.Body = textBox5.Text;
		if (listBox1.Items.Count != 0)
	{
		for (i = 0; i < listBox1.Items.Count; i++)
		mail.Attachments.Add(new Attachment(listBox1.Items[i].ToString()));
	}
		mail.IsBodyHtml = true;
		mail.DeliveryNotificationOptions = 
			DeliveryNotificationOptions.OnFailure;
		mail.ReplyTo = new MailAddress(textBox1.Text);
		SmtpServer.Send(mail);	
	}
		catch (Exception ex)
	{
		MessageBox.Show(ex.Message, "EMail", 
		    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
	}
      }
  }

Attachment Form

For the Attachment Form, make a new form and add 1 text box With Name (txtFile), and 3 buttons. The first button will have Name btnBrowse and text Browse, the second button will have Name btnOK and text OK, the third button will have Name btnCancel and text Cancel. The code for this form will be:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace smtp_csharp
	{
		/// <summary>
		/// Summary description for frmAddAttachment.
		/// </summary>
		public class frmAddAttachment : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.Button btnOK;
		internal System.Windows.Forms.Button btnBrowse;
		internal System.Windows.Forms.OpenFileDialog dlg;
		public System.Windows.Forms.TextBox txtFile;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public frmAddAttachment()
	{
		//
		// Required for Windows Form Designer support
		//
		InitializeComponent();
		//
		// TODO: Add any constructor code after InitializeComponent call
		//
	}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
	{
		if( disposing )
	{
		if(components != null)
	{
		components.Dispose();
	}
	}
		base.Dispose( disposing );
	}
		private void btnBrowse_Click(object sender, System.EventArgs e)
	{
		dlg.ShowDialog();
		txtFile.Text = dlg.FileName;
	}
		private void btnOK_Click(object sender, System.EventArgs e)
	{
		this.Hide();
	}
		private void btnCancel_Click(object sender, System.EventArgs e)
	{
		txtFile.Text = "";
		this.Hide();
	}
      }
   }

Now to send the message with Yahoo, the code will remain the same except change 2 lines of code.

The original code to be changed is:

SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;

The above code should be changed to:

SmtpServer.Host = "smtp.mail.yahoo.com";
SmtpServer.EnableSsl = false;

You can now send email via Gmail and Yahoo. Live mail is a little trickier since it uses SSL and SPA.

But we will add 2 lines of code and change 1 line of code from the original Gmail app.

The original line of code to be changed is:

SmtpServer.Host = "smtp.gmail.com";

The above code should be changed to:

SmtpServer.Host = "smtp.live.com";

Now we will add 2 lines of code to our app:

The first code should be added to the button3 click event between:

SmtpClient SmtpServer = new SmtpClient();

and:

NetworkCredential cred = new NetworkCredential(textBox2.Text, textBox3.Text);

The code to be added is:

SmtpServer.Credentials=new System.Net.NetworkCredential(textBox2.Text, textBox3.Text);

The final code to be added is:

using System.Net;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

charles henington

United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
QuestionAccess for less secure app->Turn On Pin
aazam rafiee zade18-Mar-15 4:15
memberaazam rafiee zade18-Mar-15 4:15 
QuestionMessage Automatically Removed Pin
18-Feb-14 23:46
memberMember 1014497618-Feb-14 23:46 
QuestionGreat! Just what I was looking for Pin
gcode111-Apr-13 7:09
membergcode111-Apr-13 7:09 
Questiondoes not work for LIVE Pin
zinxx8-Jan-13 3:25
memberzinxx8-Jan-13 3:25 
GeneralMy vote of 4 Pin
csharpbd19-Nov-12 21:24
membercsharpbd19-Nov-12 21:24 
GeneralMy vote of 1 Pin
ChewsHumans6-Feb-12 6:31
memberChewsHumans6-Feb-12 6:31 
GeneralMy vote of 1 Pin
dvptUml17-Apr-11 0:48
memberdvptUml17-Apr-11 0:48 
GeneralMy vote of 1 Pin
Freax Domino30-Nov-10 11:54
memberFreax Domino30-Nov-10 11:54 
GeneralRe: My vote of 1 Pin
charles henington17-Mar-11 19:33
membercharles henington17-Mar-11 19:33 
Generalvery good article thanks but can't download the code Pin
Hasbi Allah22-Oct-10 7:35
memberHasbi Allah22-Oct-10 7:35 
GeneralVery Good Pin
rspercy603-Jun-10 18:10
memberrspercy603-Jun-10 18:10 
GeneralRe: Very Good Pin
charles henington3-Jun-10 23:26
membercharles henington3-Jun-10 23:26 
GeneralHmm.. Very nice, but it's not working Pin
johnstunsky2-Jun-10 1:01
memberjohnstunsky2-Jun-10 1:01 
GeneralRe: Hmm.. Very nice, but it's not working Pin
charles henington2-Jun-10 1:25
membercharles henington2-Jun-10 1:25 
GeneralRe: Hmm.. Very nice, but it's not working Pin
johnstunsky2-Jun-10 23:10
memberjohnstunsky2-Jun-10 23:10 
GeneralRe: Hmm.. Very nice, but it's not working Pin
charles henington3-Jun-10 13:10
membercharles henington3-Jun-10 13:10 
GeneralRe: Hmm.. Very nice, but it's not working Pin
charles henington3-Jun-10 13:23
membercharles henington3-Jun-10 13:23 
GeneralMy vote of 1 Pin
muellerh26-May-10 23:31
membermuellerh26-May-10 23:31 
GeneralRe: My vote of 1 Pin
charles henington27-May-10 14:26
membercharles henington27-May-10 14:26 
AnswerRe: My vote of 1 Pin
muellerh27-May-10 21:38
membermuellerh27-May-10 21:38 
GeneralRe: My vote of 1 Pin
charles henington28-May-10 0:03
membercharles henington28-May-10 0:03 
GeneralRe: My vote of 1 Pin
daveauld8-Sep-10 20:29
memberdaveauld8-Sep-10 20:29 
GeneralMy vote of 1 Pin
mail-2226-May-10 21:40
membermail-2226-May-10 21:40 
GeneralRe: My vote of 1 Pin
charles henington27-May-10 14:25
membercharles henington27-May-10 14:25 
GeneralCode formatting Pin
spencepk12-May-10 10:28
memberspencepk12-May-10 10:28 
GeneralRe: Code formatting Pin
charles henington27-May-10 14:37
membercharles henington27-May-10 14:37 
GeneralMy vote of 1 Pin
Yahia Alhami9-May-10 4:02
memberYahia Alhami9-May-10 4:02 
GeneralRe: My vote of 1 Pin
charles henington9-May-10 12:14
membercharles henington9-May-10 12:14 
GeneralRe: My vote of 1 Pin
charles henington12-May-10 6:55
membercharles henington12-May-10 6:55 
Generalvery nice, thank you Pin
Mohammad Elsheimy8-May-10 3:09
memberMohammad Elsheimy8-May-10 3:09 
GeneralRe: very nice, thank you Pin
charles henington8-May-10 22:40
membercharles henington8-May-10 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 27 May 2010
Article Copyright 2010 by charles henington
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid