• (3 Pages)
  • +
  • 1
  • 2
  • 3

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

#1 PixelCard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 139
  • Joined: 20-June 08

Posted 12 July 2008 - 07:00 AM

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:

// 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:

Posted Image

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

Posted 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:

Posted 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:

Posted 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):


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:


// 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)



Is This A Good Question/Topic? 4
  • +

Replies To: Sending e-mail messages using C#

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 13 July 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.
Was This Post Helpful? 0
  • +
  • -

#3 PixelCard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 139
  • Joined: 20-June 08

Posted 13 July 2008 - 11:47 AM

View Postborn2c0de, on 13 Jul, 2008 - 06:25 AM, said:

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).
Was This Post Helpful? 0
  • +
  • -

#4 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 01 August 2008 - 08:28 AM

There is only one problem with this.

The problem is that people can use it to fake emails.
Was This Post Helpful? 0
  • +
  • -

#5 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Posted 01 August 2008 - 08:30 AM

View Postgbertoli3, on 1 Aug, 2008 - 11:28 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#6 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 01 August 2008 - 09:16 AM

View Postmarcells23, on 1 Aug, 2008 - 08:30 AM, said:

View Postgbertoli3, on 1 Aug, 2008 - 11:28 AM, said:

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: 01 August 2008 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#7 IanMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-August 08

Posted 17 August 2008 - 07:45 AM

View Postgbertoli3, on 1 Aug, 2008 - 09:16 AM, said:

View Postmarcells23, on 1 Aug, 2008 - 08:30 AM, said:

View Postgbertoli3, on 1 Aug, 2008 - 11:28 AM, said:

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:

Cheers
Ian

This post has been edited by IanMc: 17 August 2008 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Posted 26 August 2008 - 09:57 PM

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

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



Was This Post Helpful? 0
  • +
  • -

#9 csharp3r  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 10-November 08

Posted 10 November 2008 - 03:10 PM

can someone please explain how to use smtp-server? I mean give an example please for hotmail or gmail.. thx in advance
Was This Post Helpful? 1
  • +
  • -

#10 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 13 November 2008 - 06: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)
Was This Post Helpful? 1
  • +
  • -

#11 csharp3r  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 10-November 08

Posted 19 November 2008 - 01:02 PM

View Postborn2c0de, on 13 Nov, 2008 - 05:08 AM, said:

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 November 2008 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#12 viswa.teja@live.in  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-September 09

Posted 03 December 2009 - 01:30 AM

How to receive mails and read them in Rich text box please help me its urgent.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Tanner*


Reputation:

Posted 28 February 2010 - 07:39 PM

Could you post a picture of the form filled out with a email ready to send to "someone@gmail.com" ?
I can't seem to get the wright info into the boxes or something because its not sending.

Thanks, Tanner.
Was This Post Helpful? 0

#14 Guest_vijay singh*


Reputation:

Posted 25 March 2010 - 04:27 AM

how we can send richtext box text in html format through mail
Was This Post Helpful? 0

#15 Guest_Guest*


Reputation:

Posted 01 April 2010 - 01:44 AM

what to write in send server?
Was This Post Helpful? 0

  • (3 Pages)
  • +
  • 1
  • 2
  • 3