Recent Posts

Thursday, July 10, 2008

Send Mail through .Net Application

Get free code to send mail through .net application, provides all the required functionality to send email from vb net through smtp. It can be modified into c# and can be used for all your .net desktop and asp .net applications.

' First import system web mail
Imports System.Web.Mail

Private Function SendMail()


Dim oMsg As MailMessage = New MailMessage

' TODO: Replace with sender e-mail address.
oMsg.From = ""
' TODO: Replace with recipient e-mail address.
oMsg.To = ""
oMsg.Subject = "Test Mail"

' Send email in html format (comment this line to send plain text email).
oMsg.BodyFormat = MailFormat.Html

'HTML Body (remove HTML tags for plain text email).
oMsg.Body = "Hello World!"

' Add an attachment and send email
' TODO: Replace with path to attachment.
Dim sFile As String = "C:\Hello.txt"
Dim oAttch As MailAttachment = New MailAttachment(sFile, MailEncoding.Base64)


' TODO: Replace with the name of your remote SMTP server.
SmtpMail.SmtpServer = "your SMTP Server" 'put your smtp server name here


oMsg = Nothing
oAttch = Nothing

MessageBox.Show("Email sent successfully")

Catch ex As Exception
Dim strMsg As String
strMsg = (("The following exception occurred: " + ex.ToString()))
'check the InnerException
While Not (ex.InnerException Is Nothing)
strMsg = strMsg + vbCrLf + ("--------------------------------")
strMsg = strMsg + vbCrLf + (("The following InnerException reported: " + ex.InnerException.ToString()))
ex = ex.InnerException
End While
End Try

End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call SendMail1()
End Sub

Related Posts by Categories

No comments:

Post a Comment