Sunday, March 27, 2011

1.1 If and Else Statements - SMTP program


If and Elseif

If you have ever programmed before, you will know what these are, if not, don't worry, you will soon understand.

You should be viewing the basic code of your program at the moment, including the message box we made earlier and the "Sub" for button1. Highlight the message box code ( MessageBox.Show("Hello World") ) And delete it.

In the sub for the button type this:


Code:
If TextBox1.Text = "Andy" = True Then
Click Return (Enter)
You should now be on a new line with an indent, type.
Code:
MessageBox.Show("Correct")

This tells the program that if the text (.Text) In text box1 (TextBox1) says "Andy" Then it should display a message box saying "Correct"

Now we need to tell it what to do if the user types something else.
Hit return again and type.
Code:
ElseIf TextBox1.Text = "Andy" = False Then
Hit(Enter)
Code:
MessageBox.Show("Wrong!")

An "End If" should also appear on it's own just before the end of the sub, this tells the program that we done with "If"s

Now do you understand what we have done? When the button is clicked first the program will check if the writing in the box says "Andy" if it does it will show a message box.

However "If" this is not true (Else If) Then it will check to see if it is false, in this case it will show the "Wrong!" Message box.

If you are feeling confident try changing the first bit to this.
Code:
()
            If TextBox1.Text = "Andy" Or "Skill" = True Then
Understand what I did there? I assigned two options to be true.



Email Program
Although If and ElseIf(s) are fun, are you ready to code something useful? Now we get to use some more advanced code, feel free to copy and paste this as not to make errors.

I would like to to save your form we have been using so far, and create a new one (Remember How?) Again a Windows Forms Application, and call this one Gmail. (If you don't have a Gmail account, sign up for one now (Googlemail will do too)

In your form add two text boxes, two labels and a button.

Take text box1 and put it at the top, and just above it put label1, change label1 to "Send To:"

Now take text box2 and put it bellow text box1 with enough room for label2 in between them. Put label2 between them and call it "E-Mail"

Now on text box2 hover over the top right corner and click the arrow, then check the box. You can now drag text box2 down to make it bigger, do so.

Put the button under all of this and call it "Send"

Any idea what we are doing yet? The text inside text box1 is who you want to send to, and the text inside text box2 is your message.

Now double click the button, click just before "Public Class Form1" and type.


Code:
()
Imports System.Net.Mail
Click return.

Remember those imports I talked about? That is one of them.

Now in the sub for button one put this in.
Code:
()

                Dim MyMailMessage As New MailMessage
        MyMailMessage.From = New MailAddress(Your Gmail)
                MyMailMessage.To.Add(TextBox1.Text)
        MyMailMessage.Subject = (The Alliance Mega Thread)
                MyMailMessage.Body = (TextBox2.Text)
                Dim SMTPServer As New SmtpClient("smtp.gmail.com")
                SMTPServer.Port = 587
        SMTPServer.Credentials = New System.Net.NetworkCredential(Your Gmail, Your Gmail Password)
                SMTPServer.EnableSsl = True
                SMTPServer.Send(MyMailMessage)

Now(don) 't panic, let me explain what this does.
Code:
Dim MyMailMessage As New MailMessage
This "Defines" the message.


Code:
MyMailMessage.From = New MailAddress(Your Gmail)
This is who the e-mail is from, type your Gmail in here, eg. MailAddress(Andy@gmail.com)


Code:
MyMailMessage.To.Add(TextBox1.Text)
This tells it to send the message to the e-mail written in Text Box1 (Much like in Ifs and ElseIfs, but sending mail not making a message box.)


Code:
MyMailMessage.Subject = (The Alliance Mega Thread)
This is the subject of the Email, if you wanted you could add a third text box and change that too.
 
Code:
MyMailMessage.Body = (TextBox2.Text)
This line tells the program the the "body" or main part of the e-mail is the text in Text Box2.


Code:
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
This defines the server it will be sent via, in this case Gmail (smtp.gmail.com)


Code:
SMTPServer.Port = 587
The port e-mails are normally sent through.


Code:
SMTPServer.Credentials = New System.Net.NetworkCredential(Your Gmail, Your Gmail Password)
Now this is the login information, enter your Gmail and your password, what it will do is automatically log into your account and send the mail.


Code:
SMTPServer.EnableSsl = True
This will enable SSL


Code:
SMTPServer.Send(MyMailMessage)
And lastly, and most importantly, this will send the message.

Well done, this is your first real program, save it, build it, test it (You can send Emails to yourself)

11 comments:

  1. Woah great tutorial. I will be watching this blog.

    ReplyDelete
  2. excellent tutorial, easy to understand

    ReplyDelete
  3. Really good tutorial man. I am surprised that you could make it so simple. Will be referring to here a lot now. Great blog. Good luck with future success ! Also, followed.

    ReplyDelete
  4. never programmed, something I've been interested in.
    Keep up the good stuff...maybe I'll actually take the time to learn something :)

    ReplyDelete
  5. if/else is the basic in programming.. not that difficult to understande you try

    ReplyDelete
  6. Are you going to be doing C++ tutorials? :)

    ReplyDelete
  7. Excellent post man! Was a really interesting read! thanks for sharing!

    ReplyDelete