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.
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("firstname.lastname@example.org", "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.
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.