Sunday, February 28, 2010

Send Email from VB.NET 2008

Sometimes when we open a application there a a simple menu for sending feedback about the application. actually ist to simple to add capabilty mail sending on application , especialy when we write the app on VB.net. i have implements this capabilty into my lastest project on reliez document management System.




the document which store on database can be share with other via mail or faxing. on this post itry to share how to send mail with vb.net. the code for sending mail like below :

Dim mail As New MailMessage()
mail.From = New MailAddress(TextBox2.Text)
mail.To.Add(TextBox3.Text)

mail.Subject = TextBox4.Text
mail.Body = TextBox6.Text

mail.Priority = MailPriority.High

Dim att As New System.Net.Mail.Attachment(TextBox5.Text.Trim())
mail.Attachments.Add(att)

Dim smtp As New SmtpClient(TextBox1.Text)
If CheckBox1.Checked = True Then
smtp.Credentials = New System.Net.NetworkCredential(TextBox7.Text, TextBox8.Text)
End If

Me.Cursor = Cursors.AppStarting

Try
smtp.Send(mail)
MsgBox("Message successfull send to user.", MsgBoxStyle.Information, "Send Email")
Me.Cursor = Cursors.Arrow
mail.Dispose()
GC.Collect()
Me.Close()

Catch ex As Exception

mail.Dispose()
GC.Collect()
Me.Cursor = Cursors.Arrow
MsgBox(ex.Message, MsgBoxStyle.Critical, "Mail Error")
End Try

for sample code you can download via this link.

0 komentar: