Simple ASP.net Send Email Subroutine

Discussion in 'ASP.NET Code Snips' started by BluJag, Mar 17, 2007.

  1. BluJag

    BluJag Perch

    17 March 2007

    Tested in asp.net version 1 (see next post for verson 2) Neither tried with an AOL address yet - will be interested to hear if it works OK.

    Dim MyMail As New System.Web.Mail.MailMessage
    Dim SMTP As System.Web.Mail.SmtpMail

    Dim Email as string
    Dim Subject as string
    Dim MsgTxt as string

    With MyMail

    .To = Email
    .From = "xxx@domain.com"
    .Subject = Subject
    .Body = MsgTxt
    .Bcc = "secret@anotherdomain.com"
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com"
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx@kdomain.com"
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    End With


    With SMTP
    .SmtpServer = "mail.domain.com"
    .Send(MyMail)
    End With
  2. BluJag

    BluJag Perch

    Tested in asp.net version 2

    Dim Credential As New System.Net.NetworkCredential("info@domain.com", "password")

    Dim MyMail As New System.Net.Mail.MailMessage

    Dim Client As New System.Net.Mail.SmtpClient()

    Client.Host = "mail.domain.com"


    Client.Credentials = Credential

    Dim Email As New System.Net.Mail.MailMessage
    With Email
    .From = New System.Net.Mail.MailAddress("noreply@domain.com")
    .To.Add("to@somedomain.com")
    .CC.Add("me@somedomain.com")
    .Bcc.Add("secret@domain.com")
    .ReplyTo = New System.Net.Mail.MailAddress("someotheraddress@domain.com")
    .Subject = "Your subject"
    .IsBodyHtml = False
    .Body = "Your message text etc"


    End With

    Try

    Client.Send(Email)

    Catch ex As Exception
    'some processing code here to handle errors
    End Try

Share This Page

JodoHost - 26,000 hosting end-users in 100 countries
Plesk Web Hosting
VPS Hosting
H-Sphere Web Hosting
Other Services