Tuesday 25 September 2007

sendmail

1. Add reference to System.Web.dll
Project > Add Reference... > .NET > System.Web.dll > Select > OK

2. should have smtpserver.

3. use the below code to send email.

Public Shared Sub SendMail(strFrom as String, strTo as String, strCC as String,
strSubject as String, strBody as String, strAttachments as String, strSMTPServer as String)

'send the email
Try
Dim insMail As New MailMessage()
With insMail
.From = strFrom
.To = strTo
.Subject = strSubject
.Body = strBody
.Cc = strCC
If Not strAttachments.Equals(String.Empty) Then
Dim strFile As String
Dim strAttach() As String = strAttachments.Split(";")
For Each strFile In strAttach
.Attachments.Add(New MailAttachment(strFile.Trim()))
Next
End If
End With

If Not strSMTPServer.Equals(String.Empty) Then
SmtpMail.SmtpServer = strSMTPServer
End If

SmtpMail.Send(insMail)

Catch e As Exception
Console.WriteLine(e.Message)
End Try

End Sub

No comments: