Mengirim email menggunakan vb.net

Berikut ini adalah cara mengirimkan email lewat coding menggunakan vb.net

Kita siapkan dulu namespacenya
Imports System
Imports System.Data
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Mail


Langkah pertama adalah kita cek dulu koneksi internetnya  dengan cara mengeping mbah google
   Public Shared Function CheckForInternetConnection() As Boolean
        Try
            Using client = New WebClient()
                Using stream = client.OpenRead("http://www.google.com")
                    Return True
                End Using
            End Using
        Catch
            Return False
        End Try
    End Function


Kemudian kita buat prosedur/fungsi untuk mengirim emailnya
Private sub SendEmail(ByVal emailPenerima As String, ByVal Subject As String )
       Dim emailServer As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
        Dim myEmail As String = "emailpengirim"
        Dim pass As String = "password email pengirim"
        Dim purpose As String = "emailPenerima"

        Dim mail As MailMessage = New MailMessage()
        'smtp tergantung dari smtp email dan provider internetmu, kebetulan gw pake bisnet 
        Dim SmtpServer As SmtpClient = New SmtpClient("smtp.bizmail.yahoo.com")
        mail.From = New MailAddress(myEmail)
        mail.To.Add(purpose)
        mail.Subject = Subject
        mail.IsBodyHtml = True
        mail.Body = strB.ToString
        SmtpServer.Host = "smtp.bizmail.yahoo.com"
        SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
        SmtpServer.UseDefaultCredentials = False
        SmtpServer.ServicePoint.MaxIdleTime = 1
        'port tergantung dari email pengirim
        SmtpServer.Port = 587
        SmtpServer.Credentials = New System.Net.NetworkCredential(myEmail, pass)
        SmtpServer.EnableSsl = True
        SmtpServer.Send(mail)

End sub




Private Sub btnKirim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKirim.Click
   If CheckForInternetConnection then
        SendEmail("keongjalan@yahoo.com","Halooo Keong")
  else
   messagebox.Show("Koneksi internet tidak ada, email tidak dapat dikirim","Informasi",MessageBoxButtons.OK,MessageBoxIcon.Information)
   end if
End Sub

Kamu bisa menggunakan looping untuk melakukan email sebanyak yang kamu mau..
sekian terima kasih.




Post a Comment for "Mengirim email menggunakan vb.net "