6 Replies - 3269 Views - Last Post: 21 February 2011 - 01:39 AM Rate Topic: -----

#1 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

using the System.Net.Mail namespace questions

Posted 20 February 2011 - 04:12 PM

Hello again guys!

I was reading up on how to send email from a program using System.Net.Mail namespace, and I wrote this method:

 public void CreateEmail(string From, string To, string Subject, string Body)
        {
            //create the mail message
            MailMessage mail = new MailMessage();

            //set the addresses
            mail.From = new MailAddress(From);
            mail.To.Add(To);

            //set the content
            mail.Subject = Subject;
            mail.Body = Body;

            //send the message
            SmtpClient smtp = new SmtpClient(<server address goes here>);
            smtp.Send(mail);

        }



So the first question I have is how I can find the server address? What server would I use? What if i want to send from a hotmail account?

My second question is how do I authenticate to send the email? It says I can't send the email and I know it's because I'm not authenticated. I know that you use NetworkCredentials to pass a username and password, but I don't understand what username and password I would use? My email username and password in the from field?
Please explain
Thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: using the System.Net.Mail namespace questions

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: using the System.Net.Mail namespace questions

Posted 20 February 2011 - 04:45 PM

I believe hotmail smtp server is:

Quote

smtp.live.com


I believe you connect on port 25.

The username and password would be the username and password you use to log into you hotmail email account :)

Here is a tutorial that may help you out:

http://www.dreaminco...ges-using-c%23/

This post has been edited by CodingSup3rnatur@l-360: 20 February 2011 - 04:52 PM

Was This Post Helpful? 1
  • +
  • -

#3 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: using the System.Net.Mail namespace questions

Posted 20 February 2011 - 04:56 PM

So I found this code on a website and edited it to fit my needs:
public void CreateEmail(string From, string To, string Subject, string Body)
        {
            // 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("smtp.live.com");

                // 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(From, To);

                // The message body is the message content provided in the
                // contentBox.
                message.Body = Body;

                // The message subject is located in the subjectBox.
                message.Subject = Subject;

                // 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("myemail@hotmail.com", "mypass");

                // Some servers require a specific port to connect,
                // so it is specified in the serverPort text box.
                
                client.Port = System.Convert.ToInt32(25);

                // Send the message.
                client.Send(message);
            }

            // This catches the exceptions, if any.
            catch (Exception ex)
            {
                //Show message explaining the problem
                Console.WriteLine("Cannot send message: " + ex.Message);
            }



I tried it and it says "The SMTP server requires a secure connection or the client was not authenticated. The server response was 5.2.0 Must issue a STARTTLS command first"

and I found a website that said use the port 2525 and I tried it and it just said the sending failed.

Any idea what to do now?
Was This Post Helpful? 0
  • +
  • -

#4 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: using the System.Net.Mail namespace questions

Posted 20 February 2011 - 05:13 PM

Also, when I get an email, how could I extract the body of the email to put in say a text file?
Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: using the System.Net.Mail namespace questions

Posted 20 February 2011 - 05:19 PM

Try adding this line in the try block:

client.EnableSsl = true;


and use port 25.
Was This Post Helpful? 0
  • +
  • -

#6 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: using the System.Net.Mail namespace questions

Posted 20 February 2011 - 05:35 PM

Perfect! Thanks CodingSup3rnatur@l! What about extracting the body of an email I get from using POP3?
Was This Post Helpful? 0
  • +
  • -

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: using the System.Net.Mail namespace questions

Posted 21 February 2011 - 01:39 AM

Here is a link on how to make a Pop3 class to collect and delete emails. The hotmail Pop3 server is:

Quote

pop3.live.com


and the port is 995 (or 110 according to the article below)

http://www.kbcafe.co...POP3.CSharp.pdf


Here is a link to a pre built POP3 client (download links at top of article):

http://www.codeproje...pop3client.aspx

(don't know how effective it is though ;))


You will have to use an external library, or build your own to do this :)

This post has been edited by CodingSup3rnatur@l-360: 21 February 2011 - 01:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1