Sending email from an application using VB.NET


Here is a small piece of code which can be used to send an email from an application using VB.NET. You can create a new form and add 4 labels to it and 4 text boxes to it. The labels can be named as From, To, Subject and Message respectively. Corresponding text boxes are also added and kept in position. The last text box can be made multiline. (In the Properties.)

Sending email from an application using VB.NET



Here is a small piece of code which can be used to send an email from an application using VB.NET.

You can create a new form and add 4 labels to it and 4 text boxes to it. The labels can be named as From, To, Subject and Message respectively.

Corresponding text boxes are also added and kept in position. The last text box can be made multiline. (In the Properties.)

Add a Button labelled 'Send'

In the code part of the Form, add :


Imports System.Net.Mail


Then add the following code :



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential("abc@gmail.com", "password123")
'using Gmail
smtpServer.Port = 587
smtpServer.Host = "smtp.gmail.com"
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress("abc@gmail.com", TextBox1.Text)
mail.To.Add(TextBox2.Text)
mail.Subject = TextBox3.Text
mail.Body = TextBox4.Text
smtpServer.Send(mail)
Me.Close()

End Sub


In this part smtpServer.Credentials = New Net.NetworkCredential("abc@gmail.com", "password123")

your gmail id and password should be given.

Try this.

I have attached the form design.

NB : I got this with gmail only. I tried some other mail and it did not work. If someone else know, please comment on this article.


Attachments

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: