Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,846 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,647 people online right now. Registration is fast and FREE... Join Now!




Sending e-mail messages using C#

 
Reply to this topicStart new topic

> Sending e-mail messages using C#, How to send e-mail messages using C# and WinForms.

PixelCard
Group Icon



post 12 Jul, 2008 - 06:00 AM
Post #1


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



Attached File(s)
Attached File  sendEmail.zip ( 41.82k ) Number of downloads: 312
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 13 Jul, 2008 - 05:25 AM
Post #2
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.
Go to the top of the page
+Quote Post

PixelCard
Group Icon



post 13 Jul, 2008 - 10:47 AM
Post #3
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).
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 1 Aug, 2008 - 07:28 AM
Post #4
There is only one problem with this.

The problem is that people can use it to fake emails.
Go to the top of the page
+Quote Post

marcells23
Group Icon



post 1 Aug, 2008 - 07:30 AM
Post #5
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.
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 1 Aug, 2008 - 08:16 AM
Post #6
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.

This post has been edited by gbertoli3: 1 Aug, 2008 - 08:16 AM
Go to the top of the page
+Quote Post

IanMc
*



post 17 Aug, 2008 - 06:45 AM
Post #7
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

This post has been edited by IanMc: 17 Aug, 2008 - 07:01 AM
Go to the top of the page
+Quote Post

wartech
Group Icon



post 26 Aug, 2008 - 08:57 PM
Post #8
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");  

Go to the top of the page
+Quote Post

csharp3r
*



post 10 Nov, 2008 - 02:10 PM
Post #9
can someone please explain how to use smtp-server? I mean give an example please for hotmail or gmail.. thx in advance
Go to the top of the page
+Quote Post

born2c0de
Group Icon



post 13 Nov, 2008 - 05:08 AM
Post #10
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)
Go to the top of the page
+Quote Post

csharp3r
*



post 19 Nov, 2008 - 12:02 PM
Post #11
QUOTE(born2c0de @ 13 Nov, 2008 - 05:08 AM) *

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)


For me it is like this:
Gmail: puts atleast "@gmail.com" and "@hotmail.com" in spam.. i tried with an other and it went through and was accepted..
Hotmail: Accepts anything except of mailadresses like "something@microsoft.com" (contains microsoft) if it does contain microsoft it puts it in "junk"
and yahoo I have never tried...

This post has been edited by csharp3r: 19 Nov, 2008 - 12:04 PM
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/3/08 05:57PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month