Jump to content

C# - Sending Email Through SMTP.

- - - - -

  • Please log in to reply
8 replies to this topic

#1
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Hello! Today we'll be sending emails through SMTP(Simple Mail Transfer Protocol). This is rather easy but does require a valid email account.


Before we can even start we must add the .Net and .Mail libraries.
using System.Net;
using System.Net.Mail;


This will allow us to access SMTPClient, MailMessage and NetworkCredentials, which we'll go over shortly.


Authentication.
This is where the valid email account comes into play. You must have a valid email account(with password) to give our program a place to send the mail from.
NetworkCredential cred = new NetworkCredential("YourEmailAccountAddress", "EmailAccountPass");
With our credentials filled in we can now send mail from our account, which we'll get to momentarily.


Creating an Email.
To send an email we must first create a new mailMessage(an email) to send.
MailMessage msg = new MailMessage();
Now with an email created we must add some details to it, including; To, from, subject, ect.
msg.To.Add("emailRecipient@gmail.com");  [COLOR=#006400]// Add a new recipient to our msg.[/COLOR]
msg.From = new MailAddress("YourEmailAccountAddress");   [COLOR=#006400]// Read below.[/COLOR]
msg.Subject = "A subject.";             [COLOR=#006400]       // Assign the subject of our message.[/COLOR]
msg.Body = "Hello, this is my message.";   [COLOR=#006400]// Create the content(body) of our message. [/COLOR]
All of the above is very self explanatory. As for the .From property, it can be changed from you email address, but when the email is opened it will still read as the email address of the account from which the email is sent.
If you dont understand, lets say the details are filled out as below.
NetworkCredential cred = new NetworkCredential("prankster@gmail.com", "EmailAccountPass");
msg.From = new MailAddress("CrazyGuy@insane.com")

When you recive the email, it will still say from "Prankster@gmail.com" because thats where its sent from, we just arnt allowed to send an email without filling in its .From property.


Sending the email.
Now we must create a new Smtp client to send our email.
SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
We created a new client which has two parameters (Host, Port). The host is where we'll be sending the mail from, this should be the same as your email account. I have a Gmail account, so I use "smtp.gmail.com" as my host. The only reason we use port 25 is because it's the defualt Smtp port, although if you wish, you may use another port.
(At the end of the tutorial I will post alternative hosts.)


Now we fill in the client details and send the email.
client.Credentials = cred; // Send our account login details to the client.
client.EnableSsl = true;   // Read below.
client.Send(msg);          // Send our email.
Enabling SSL(Secure Sockets Layer, encyription) is reqiured by most email providers to send mail, thus we the reason we enable it. :)


Run your program and check your email, if all is well you should see.
Attached File  emailOutput.png   48.89K   163 downloads




Host List:
smtp.gmail.com // Gmail
smtp.live.com // Windows live / Hotmail
smtp.mail.yahoo.com // Yahoo
smtp.aim.com // AIM
my.inbox.com // Inbox
( I havent tested them all so just let me know if one of them doesn't work. :) )


I think that covers most things, I hope this tutorial was short and sweet. Any comment, questions, or rep welcome.

Thanks ~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#2
Jeremy Morgan

Jeremy Morgan

    Newbie

  • Members
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon
Thanks for posting, this is a very clean tutorial.

I built something like this a while back. As a note if you're doing this, make sure to capture and suppress any error output from this, as you don't want your credentials or other pertinent information being shown if an error occurs outside your control.

Good job!

#3
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Thank you very much Jeremy, I'm glad you like it. Good point about making sure error's are taken care of, thanks for posting.

~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Maybe in another tutorial you could explain how to check email, if you haven't already (I didn't check all your tutorials, so I don't know whether you made one about that or not); well, you can't make any more tutorials for the submission forum until the next contest, obviously, but I mean just any other tutorial.

But yeah, it's a nice tutorial; +rep for your contribution.

#5
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Nope don't got one for checking email. I'll see what I can do, but I got a lot of school work starting this week. :rules:

Thanks for the rep! :)
~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#6
terroare

terroare

    Newbie

  • Members
  • PipPip
  • 16 posts
Thank you for saring.
Keep up the good work :thumbup:

#7
Cushpajz

Cushpajz

    Newbie

  • Members
  • PipPipPip
  • 30 posts
  • Location:Novi Sad
  • Programming Language:C#
  • Learning:C, C++, C#, PHP
It's good, but maybe you can put 2 more textboxes for credentials, this is just for 1 user, but it's a good tutorial :)

#8
Jeremy Morgan

Jeremy Morgan

    Newbie

  • Members
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon

Cushpajz said:

It's good, but maybe you can put 2 more textboxes for credentials, this is just for 1 user, but it's a good tutorial :)

That's a good idea. I think the general purpose of the tutorial is to show how to interact with the STMP functions, and the best way to learn is start experimenting with is code. This code can be easily modified to take inputs and use them to send the mail using your account.

Ask yourself:

Where are SMTP credentials being used in this code?
How can I change them?
How can I add inputs and then map them to the credentials used in this code?



By asking yourself these questions and making modifications you'll learn how basic web apps work. Instead of asking him to write the code for you, do it yourself and you'll learn a lot. I say this with respect, as I feel you'll benefit more by figuring this stuff out than you will by having him update the code with all the answers.

#9
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts

Quote

That's a good idea. I think the general purpose of the tutorial is to show how to interact with the STMP functions, and the best way to learn is start experimenting with is code. This code can be easily modified to take inputs and use them to send the mail using your account.

I agree. :)

Quote

It's good, but maybe you can put 2 more textboxes for credentials, this is just for 1 user, but it's a good tutorial

It shouldn't be to hard at all to throw in some textBoxes. :)
Good luck ~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users