Full Version: Sending e-mail messages using C#
Dream.In.Code > Programming Tutorials > C# Tutorials
PixelCard
In this tutorial I will show how to send e-mail messages from a C# application.

Tutorial Requirements:
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 2.0

For this application it is needed to declare one additional namespace:

csharp

// Accessing an additional namespace for mail sending.
using System.Net.Mail;


So, here are the steps needed to be performed:

1. Create a standard C# Windows Forms application:

IPB Image

2. Add some labels and text boxes and two buttons to the form, so the form looks like this:

IPB Image

As you see, the current text in the text boxes is the name of each text box. You have to change the names to the corresponding boxes to make the code work. However, you can leave the default names, but don't forget to change the code.

3. Change the passBox PasswordChar property to '*', so when you will enter the password for the mail box, no one could see it:

IPB Image

4. For the contentBox change the AcceptsReturn and AcceptsTab properties to true, so when you will enter the message, you can use the TAB and ENTER keys to format the text:

IPB Image

Now, when the form is ready, let's pass to the code.


1. As the System.Net.Mail namespace was decalred at the beginning of the tutorial, we can pass to the sending code (for the Send button):


csharp

private void button1_Click(object sender, EventArgs e)
{
// To avoid situations, when the program crashes because server rejection or
// invalid data, an exception handling mechanism is created.
try
{
// Creating a new SMTP Client. The server URL/IP is indicated as
// sendServer.Text (that is the text box with the data).
SmtpClient client = new SmtpClient(sendServer.Text);

// Creating a new mail message. The sender and receiver are
// indicated as sendFrom.Text and SendTo.Text
// (these are the text boxes with the data).
MailMessage message = new MailMessage(sendFrom.Text, sendTo.Text);

// The message body is the message content provided in the
// contentBox.
message.Body = contentBox.Text;

// The message subject is located in the subjectBox.
message.Subject = subjectBox.Text;

// To be able to send the message, it is necessary to provide the
// credentials on the server. The username is located in the userBox
// and the password is located in the passBox.
client.Credentials = new System.Net.NetworkCredential(userBox.Text, passBox.Text);

// Some servers require a specific port to connect,
// so it is specified in the serverPort text box.
if (serverPort.Text != null)
client.Port = System.Convert.ToInt32(serverPort.Text);

// Send the message.
client.Send(message);
}

// This catches the exceptions, if any.
catch (Exception ex)
{
// Show a message, explaining the problem.
MessageBox.Show("Cannot send message: " + ex.Message);
}
}


2. There is also code for the 'Clear Fields' button:


csharp

// Clear every field.
sendServer.Clear();
serverPort.Clear();
sendTo.Clear();
sendFrom.Clear();
userBox.Clear();
passBox.Clear();
subjectBox.Clear();
contentBox.Clear();


The application is ready.

This application is using SMTP (Simple Mail Transfer Protocol) to send messages, so you must have access toy uor mail provider's SMTP server (which is available for most mail services). The SMTP server address usually looks like this:

smtp.yourmailprovider.domain

The most commonly used SMTP port is:

2525

born2c0de
QUOTE
The most commonly used SMTP port is:
2525

Actually the most common SMTP port is still 25, but many are switching to 2525 for a reason that I'm unaware of.
PixelCard
QUOTE(born2c0de @ 13 Jul, 2008 - 06:25 AM) *

QUOTE
The most commonly used SMTP port is:
2525

Actually the most common SMTP port is still 25, but many are switching to 2525 for a reason that I'm unaware of.


This is why I indicated the 2525 port (I also tested the application on port 2525).
gbertoli3
There is only one problem with this.

The problem is that people can use it to fake emails.
marcells23
QUOTE(gbertoli3 @ 1 Aug, 2008 - 11:28 AM) *

There is only one problem with this.

The problem is that people can use it to fake emails.


that all depends on the smtp/exchange server not on the application.
gbertoli3
QUOTE(marcells23 @ 1 Aug, 2008 - 08:30 AM) *

QUOTE(gbertoli3 @ 1 Aug, 2008 - 11:28 AM) *

There is only one problem with this.

The problem is that people can use it to fake emails.


that all depends on the smtp/exchange server not on the application.


Yeah your right but what I am saying is that you are able to fake an email.
IanMc
QUOTE(gbertoli3 @ 1 Aug, 2008 - 09:16 AM) *

QUOTE(marcells23 @ 1 Aug, 2008 - 08:30 AM) *

QUOTE(gbertoli3 @ 1 Aug, 2008 - 11:28 AM) *

There is only one problem with this.

The problem is that people can use it to fake emails.


that all depends on the smtp/exchange server not on the application.


Yeah your right but what I am saying is that you are able to fake an email.


Yes this is true, you can send emails from for example bill.gates@microsoft.com but you can do this with any email program anyway.
You can do it with Outlook Express, just change the details.

pirate.gif

Cheers
Ian
wartech
Thanks for the tutorial. It was very easy to understand. Once suggestion if I may (no biggie either way)....

CODE

// Send the message.
client.Send(message);
//How about a message box stating message was sent successfully.
MessageBox.Show("Message successfully sent");  

csharp3r
can someone please explain how to use smtp-server? I mean give an example please for hotmail or gmail.. thx in advance
born2c0de
QUOTE
Yeah your right but what I am saying is that you are able to fake an email.

It doesnt matter even if you can fake an email because email servers can figure out if the email is legitimate or not.

Let's take an example:
You are sending an email as someone@gmail.com.
When the email is sent, your IP Address is attached in the email header.
The receiving mail server will perform a reverse DNS Lookup on the IP Address in the mail header (which is your IP) and check if it really is from gmail.com

Since there is no way your IP Address would resolve to gmail.com, it is marked as SPAM and sent to the Junk Folder or rejected there itself.

I performed a few tests on fake emails a few months back and this is what I could conclude from them:
1) GMail : Accepts email but stores in Junk.
2) Hotmail : Rejects Email straight away.
3) Yahoo : Allows all emails, real or fake. (Yes, LOL)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.