Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# - Sending Email Through SMTP.

authentication mailmessage

  • Please log in to reply
11 replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 17 August 2011 - 06:50 AM

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.
emailOutput.png




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.
  • 2
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 Jeremy Morgan

Jeremy Morgan

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon

Posted 17 August 2011 - 02:40 PM

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!
  • 0

#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 17 August 2011 - 03:24 PM

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. :)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 28 August 2011 - 05:42 PM

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.
  • 0

#5 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 28 August 2011 - 07:26 PM

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.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#6 terroare

terroare

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:C#

Posted 30 September 2011 - 02:21 AM

Thank you for saring.
Keep up the good work :thumbup:
  • 0

#7 Cushpajz

Cushpajz

    CC Regular

  • Member
  • PipPipPip
  • 32 posts
  • Location:Novi Sad
  • Programming Language:C#
  • Learning:C, C++, C#, PHP

Posted 17 December 2011 - 02:58 AM

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 :)
  • 0

#8 Jeremy Morgan

Jeremy Morgan

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon

Posted 17 December 2011 - 11:50 AM

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.
  • 0

#9 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 17 December 2011 - 12:35 PM

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

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.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#10 wadwmarmal

wadwmarmal

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 30 April 2013 - 08:36 AM

You can send email without smtp ... instead of using Collaboration Data Objects

 

(CDO ) .  Follow the link , it is simple and usefull when yo face any SMTP problem.

 

http://csharp.net-in...p-cdo-email.htm

 

wade.


  • 0

#11 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 30 April 2013 - 09:40 AM

CommitedC0der, question for you. I am using @studentpartner.com mail which is also by Microsoft (Microsoft Student Partners mail). Can I use that mail in this example?


  • 0

Microsoft Student Partner, Microsoft Certified Professional


#12 Mukesh

Mukesh

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 03 October 2013 - 07:04 AM

Hello All,

 

I Have followd  the step of sening email through smtp.

 

i coded like below

 

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        NetworkCredential cred = new NetworkCredential("info@jjinteriorsdelhi.com", "xxxxxxx");
        MailMessage Msg = new MailMessage();
        Msg.To.Add("mukeshbhagat4u@hotmail.com");
        Msg.From = new MailAddress("info@jjinteriorsdelhi.com");
        Msg.Subject = "Enquiry for Interior Design";
        string sMessage = "Dear  Admin, <br /><br />"
        + "Another user seeking for interior design through www.jjinterirorsdelhi.com<br /><br />";
        sMessage += "<u>Following are the User details :</u><br /><br /> Name: " + txtName.Text
        + "<br />Email: " + txtEmail.Text
        + "<br />Phone: " + txtPhone.Text
        + "<br />Message: " + txtMsg.Text;
        Msg.Body = sMessage;
        SmtpClient client = new SmtpClient("smtp.live.com", 995);
        client.Credentials = cred;
        client.EnableSsl = true;
        client.Send(Msg);
        lblSendMsg.Visible = true;
        lblSendMsg.Text = "Your Enquiry has been sent sucessfully. Thank you";
    }

 

Then it throw error like below--

 

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 65.55.162.200:995

 

how can i Solve it.

 

 


  • 0





Also tagged with one or more of these keywords: authentication, mailmessage

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download