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.MailPrivate Function SendMail()
Try
Dim oMsg As MailMessage = New MailMessage
' TODO: Replace with sender e-mail address.
oMsg.From = "fromtest@mail.com"
' TODO: Replace with recipient e-mail address.
oMsg.To = "Tomailto:Totest@mail.com"
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)
oMsg.Attachments.Add(oAttch)
' TODO: Replace with the name of your remote SMTP server.
SmtpMail.SmtpServer = "your SMTP Server" 'put your smtp server name here
SmtpMail.Send(oMsg)
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
MessageBox.Show(strMsg)
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
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)
oMsg.Attachments.Add(oAttch)
' TODO: Replace with the name of your remote SMTP server.
SmtpMail.SmtpServer = "your SMTP Server" 'put your smtp server name here
SmtpMail.Send(oMsg)
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
MessageBox.Show(strMsg)
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
No comments:
Post a Comment