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