how to send mail

krishnandu.sarkar

Simply a DIGITian
Staff member
Code:
Imports System.Net.Mail

Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim message As String

SmtpServer.Credentials = New Net.NetworkCredential(<EMAILID>,<PASSWORD>)
'Only Needed if Your SMTP Port is Not the Default One.
SmtpServer.Port = 587
SmtpServer.Host = <MAIL SERVER> (Eg. mail.yourdomainname.com)
mail = New MailMessage()
mail.From = New MailAddress(<EMAILID>)
mail.To.Add(reciever)
mail.Subject = <SUBJECT>

message = "Hello World"

mail.Body = message

'This blocks main thread until mail is sent
'SmtpServer.Send(mail)
'MsgBox("mail send")

Dim userState As Object = mail

'Do this if you want to handle event manually, otherwise follow the below statement for sending mail asynchronously i.e. by not blocking the main thread.

'wire up the event for when the Async send is completed
'AddHandler SmtpServer.SendCompleted, AddressOf SmtpClient_OnCompleted

SmtpServer.SendAsync(mail, userState)

Hope that helps...

The code is in VB.NET, but in C# the procedure is same.

BTW, A simple google reveals...
1. http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

2. http://asp.net-tutorials.com/misc/sending-mails/
 
Top Bottom