tags:

views:

59

answers:

1

Vb.net program that sends email? Can we do that?

A: 

Hi , Send email in vb.net

Or

Imports System.Net.Mail
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New _
              Net.NetworkCredential("[email protected]", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail.From = New MailAddress("[email protected]")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
Pandiya Chendur
Looks about right, although I would suggest `Using mail As New MailMessage()` since MailMessage implements IDisposable
MarkJ
Also, the code instantiates a new MailMessage twice.
Meta-Knight
this is the typical coding-in-the-dark style of programming- banging through exceptions until it "works". generally youll see every method start with try and end with catching the base exception. we had one developer who would do. catch ex as exception... response.write("<script>alert('" </script>") in every method. users loved seeing those. as an added bonus, if there was a single quote in the exception message, there would be a js error and the page wouldnt execute correctly
Shawn Simon