Sunday, 12 August 2012

send an email using only code in vb.net

In this tutorial, we will learn how to send an email using only code. The example in this tutorial will show you how to send an email by connecting to the googlemail SMTP server. If you want to send an email using another service such as hotmail or yahoomail, you will need to google for the SMTP host and port for that particular service. Modify the code like this give text boxes for subject, add, from etc. First line of code is declare mail as mail message, next line is subject of sending mail, next line enter your mail, and to mail, and body of the mail. Next line is setup smtpclient of google and send mail Copy and paste below Code: Imports System.Net.Mail
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Mail.Subject = "test email"
        Mail.To.Add("youremail@googlemail.com")
        Mail.From = New MailAddress("youremail@googlemail.com")
        Mail.Body = "This is an ownage email using VB.NET"

        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("username", "password")
        SMTP.Port = "587"
        SMTP.Send(Mail)
    End Sub

End Class