Tuesday, January 12, 2010

Send an Email via your own host - vb.net

'create instance of MailMessage

Dim mailMessage As New MailMessage()

Try

set smtp authntication for your host- this must have username(full email address. Ex admin@myhost.com) and password which is already registerd in your host

mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1)

mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "admin@myhost.com")

mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "adminpassword")

'set send from email addrss

mailMessage.From = "fromemail@myhost.com"

'recipient's email address

mailMessage.To = "ruwansj@gmail.com"

'email address of the Cc recipient

mailMessage.Cc = ""

'email address of the Bcc recipient

mailMessage.Bcc = ""

'subject of the email message

mailMessage.Subject = "Hello"

'message text format. Can be text or html

mailMessage.BodyFormat = MailFormat.Text

'message body (if you suppose to use html as bodyformat, then you have to use html tags for create mail message.

mailMessage.Body = "Test email for vb.Net ."

'email priority. Can be low, normal or high

mailMessage.Priority = MailPriority.Normal

'mail server used to send this email. modify this line based on your mail server

SmtpMail.SmtpServer = "mail.myhost.com"

'using the static method "Send" of the SmtpMail class to send the mail

SmtpMail.Send(mailMessage)


Catch ex As Exception

Response.Write(ex.Message)

End Try

No comments:

Post a Comment