Archive for June, 2008

Membuat email Notification

Dalam pembuatan program serign kali kita membuatuhkan beberapa notifikasi.
Notifikasi bisa di taruh dalam log file atau email. Namun sebaiknya notifikasi menggunakan email, karena user dapat dengan mudah membaca email tersebut.
Dalam dunia .net mudah sekali membuat(mengirim) email
Berikut Contohnya:
Imports System.Net.Mail
Module Module1
Sub Main()
SendEmail("aaa@hgs.com;bbb@hgs.com", "test subject", "body message", "C:\test.txt")
End Sub
Private Function SendEmail(ByVal iEmailaddr As String, ByVal iSubject As String, ByVal ibodymessage As String, ByVal iFullpathAttachment As String) As Boolean
Dim avalue As Boolean = False
Dim aMailMsg As MailMessage = Nothing
Dim aMailAttachment As Attachment = Nothing
Dim aError As String = String.Empty
Dim aListEmail() As String = Split(iEmailaddr, ";")
Dim aidx As Integer
Dim ClientAddress As New MailAddress("sendermail")
' Dim DestAddress As New MailAddress(iEmailaddr)
Try
aMailMsg = New MailMessage
'aMailMsg.Sender = ClientAddress
For aidx = 0 To aListEmail.Length - 1
aMailMsg.To.Add(aListEmail(aidx))
Next
aMailMsg.From = ClientAddress
aMailMsg.Subject = iSubject
aMailMsg.Body = ibodymessage
aMailAttachment = New Attachment(iFullpathAttachment)
aMailMsg.Attachments.Add(aMailAttachment)
'bila smtp server menggunakan user authotification
Dim aClient As SmtpClient = New SmtpClient("ipsmtp")
Dim aCred As New Net.NetworkCredential("user mail sender", "passwdmail")
'Add credentials if the SMTP server requires them
aClient.Credentials = aCred
aClient.Send(aMailMsg)
aMailMsg.Dispose()
avalue = True
Catch ex As Exception
Console.WriteLine("error send email : " & ex.Message)
End Try
Return avalue
End Function
End Module

Leave a Comment