Send email in VB.NET using SMTP client



Send email in VB.NET using SMTP client

Imports System.Net.Mail  'SMTP client 
Private Sub btnSendEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendEmail.Click
    Dim Msg As New System.Web.Mail.MailMessage()
    Msg.To = txtTo.Text
    Msg.From = txtFrom.Text
    Msg.Cc = txtCc.Text
    Msg.Subject = "SyntaxHelp Welcomes You"
    Msg.objEmail.Body = txtBody.Text
    Msg.Priority = MailPriority.High
    Msg.BodyFormat = MailFormat.Html
    Msg.Attachments.Add(Server.MapPath("SyntaxHelp.doc"))
    'Specify the body format
'if HTML format is checked 
    If chkFormat.Checked = True Then
        Msg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    Else
        Msg.BodyFormat = MailFormat.Text
    End If
    'Specify the body format
    If chkFormat.Checked = True Then
        Msg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    Else
        Msg.BodyFormat = MailFormat.Text
    End If
    'If you want you can add a reply to header 
    Msg.Headers.Add("Reply-To", "support@syntaxhelp.com")
    'custom headers can be added like this
    Msg.Headers.Add("SyntaxHelp", "Demo-Header")

    'Notifies if delivery of e-mail is success
    DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
    'Variable which will send the mail
    Dim client As SmtpClient
    client.Timeout = 500
    Try
        client.Send(Msg)
        Response.Write("Your Email has been sent sucessfully")

    catch (exc as Exception)
        Response.Write(" Send Email fails: " + exc.ToString())

End Sub

Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.