Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Is there a relatively simple way to sent an email from VB.Net code? I just need a light-duty way to send a simple e-mail (no attachments) using the default mail handler on a PC.

 

(I've seen a few relatively heavy-duty (complex) methods in my research but don't want or need the complexity).

 

Thanks :)

Mark

Posted

Try this, it's a simple class I made.

 

Imports System.Web.Mail

Public Class clsEmail

   Private strSubject As String
   Private strBody As String
   Private strBodyFormat As MailFormat
   Private strTo As String
   Private strFrom As String
   Private strCc As String
   Private strBcc As String
   Private strSMTPServer As String

   Friend Property Subject() As String
       Get
           Return strSubject
       End Get
       Set(ByVal Value As String)
           strSubject = Value
       End Set
   End Property

   Friend Property Body() As String
       Get
           Return strBody
       End Get
       Set(ByVal Value As String)
           strBody = Value
       End Set
   End Property

   Friend Property BodyFormat() As MailFormat
       Get
           Return strBodyFormat
       End Get
       Set(ByVal Value As MailFormat)
           strBodyFormat = Value
       End Set
   End Property

   Friend Property ToAddress() As String
       Get
           Return strTo
       End Get
       Set(ByVal Value As String)
           strTo = Value
       End Set
   End Property

   Friend Property FromAddress() As String
       Get
           Return strFrom
       End Get
       Set(ByVal Value As String)
           strFrom = Value
       End Set
   End Property

   Friend Property CcAddress() As String
       Get
           Return strCc
       End Get
       Set(ByVal Value As String)
           strCc = Value
       End Set
   End Property

   Friend Property BccAddress() As String
       Get
           Return strBcc
       End Get
       Set(ByVal Value As String)
           strBcc = Value
       End Set
   End Property

   Friend Property SMTPServer() As String
       Get
           Return strSMTPServer
       End Get
       Set(ByVal Value As String)
           strSMTPServer = Value
       End Set
   End Property

   Friend Function SendEmail() As Boolean

       Dim objEmailMessage As MailMessage
       Dim objSMTP As SmtpMail

       Try
           objEmailMessage = New MailMessage

           'Create the email
           With objEmailMessage
               .To = strTo
               .From = strFrom
               .Cc = strCc
               .Bcc = strBcc
               .Subject = strSubject
               .Body = strBody
               .BodyFormat = strBodyFormat
           End With

           'Send the email
           objSMTP.SmtpServer = strSMTPServer
           objSMTP.Send(objEmailMessage)
           SendEmail = True

       Catch ex As Exception
           SendEmail = False 'return false
       End Try

   End Function

   Public Sub New()
       'default the body format to HTML
       strBodyFormat = MailFormat.Html
   End Sub
End Class

Posted

Thanks Sam.

 

I plugged it into my program and the Import for System.Web.Mail doesn't resolve. I get: 'Namespace or type "Mail' for the Imports 'System.Web.Mail' cannot be found."

 

I also get message for one of the types: "Type 'MailFormat' is not defined." but probably due to the above.

 

Do I need to reference the 'System.Web.Mail' namespace other than the Include to get it into my program? How to do?

 

Mark

Posted

WAY COOL!

 

Everything looks good so far. All I need to do is identify what my smtpserver is and emails will fly! :)

 

Thank you for your help Sam :)

 

Mark

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...