Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updated - Send Email in VB.Net 2005 1

Status
Not open for further replies.

Lennieboy

Programmer
Jul 13, 2006
18
0
0
GB
It seems that some of the previous samples are now outdated and the System.web.mail class is now obsolete.

I will try to make is simple and easy to use:

I created this class: You can copy and paste it into a new class "Emailer"

Imports System.net.Mail


Public Class Emailer

Public Sub SendEmailMessage(ByVal sMsg As String, ByVal sTo As String, ByVal sSize As String)
'This procedure takes string array parameters for multiple recipients and files
Try

Dim MailMsg As New MailMessage(New MailAddress("Me@myplace.com"), New MailAddress(0))
MailMsg.IsBodyHtml = True
MailMsg.Subject = "Urgent - My Subject"
MailMsg.Body = sMsg.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True

'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)

'Message Successful
Catch ex As Exception
'Message Error
End Try
End Sub


End Class


With this you can call the class and send your email as required.

Let me know if you found this useful...

Darn I hate incomplete and old useless code samples!



Lennieboy -
Life is Good and God is Good
B-)
 

Your SendEmailMessage accepts sMsg As String which you use, but also requires sTo As String and sSize As String, which do what? They are never get used....

And what about Cc: and Bcc: and Attachments?

But I am with you: "Darn I hate incomplete [...] code samples!"


Have fun.

---- Andy
 
Hi Andy

I looked for proper decent WORKING code samples and found this one.

it is easy to work with and do small modifications from here...

I had to add the From and To in:

Dim MailMsg As New MailMessage(New MailAddress("myname@myplace.com"), New MailAddress(yourname@yourplace.com))

Len

Lennieboy -
Life is Good and God is Good
B-)
 
Here is the sample I found elsewhere:

Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal file As String)
'This procedure overrides the first procedure and accepts a single
'string for the recipient and file attachement
Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True

If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If

'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
'Message Error
End Try
End Sub

Len

Lennieboy -
Life is Good and God is Good
B-)
 
Here is how to add an Bcc and a CC address:

MailMsg.Bcc.Add(New MailAddress("someone@someplace.com"))

MailMsg.cc.Add(New MailAddress("someone@someplace.com"))

Lennieboy -
Life is Good and God is Good
B-)
 
Building off your examples I have created a full class. Ready to go for those that would like. I'll take any suggestions or comments you all may have.

Code:
Imports System.Net.Mail
Imports Microsoft.VisualBasic.FileIO.FileSystem

Public Class SendMail
    Private mFrom As MailAddress
    Private Mail As MailMessage
    Private sFrom As String = ""
    Private sHostServer As String = ""
    Private sSubject As String = ""
    Private ErrNum As Long = 0
    Private ErrDesc As String = ""

    Public Property Attachments() As String
        Get
            Attachments = Mail.Attachments.ToString
        End Get
        Set(ByVal value As String)
            If FileIO.FileSystem.FileExists(value) = True Then
                Dim sAttch As New Attachment(value)
                Mail.Attachments.Add(sAttch)
            Else
                ErrNum = -1
                ErrDesc = "File does not exist. Verify path and location."
            End If
        End Set
    End Property

    Public Property BCC() As String
        Get
            BCC = Mail.Bcc.ToString
        End Get
        Set(ByVal value As String)
            If value.Contains("@") And value.Contains(".") Then
                Mail.Bcc.Add(value.Trim)
            Else
                ErrNum = -1
                ErrDesc = "Invalid email address given."
            End If
        End Set
    End Property

    Public Property Body() As String
        Get
            Body = Mail.Body
        End Get
        Set(ByVal value As String)
            Mail.Body = value.Trim
        End Set
    End Property

    Public Property CC() As String
        Get
            CC = Mail.CC.ToString
        End Get
        Set(ByVal value As String)
            If value.Contains("@") And value.Contains(".") Then
                Mail.CC.Add(value.Trim)
            Else
                ErrNum = -1
                ErrDesc = "Invalid email address given."
            End If
        End Set
    End Property

    Public Sub Clear()
        Mail.To.Clear()
        Mail.CC.Clear()
        Mail.Bcc.Clear()
        Mail.Body = ""
        Mail.Subject = ""
        Mail.Attachments.Clear()
        mFrom = Nothing
        sHostServer = ""
        ErrNum = ""
        ErrDesc = ""
    End Sub

    Public Sub ClearErr()
        ErrNum = 0
        ErrDesc = ""
    End Sub

    Public ReadOnly Property ErrorNumber() As Long
        Get
            ErrorNumber = ErrNum
        End Get
    End Property

    Public ReadOnly Property ErrDescription() As String
        Get
            ErrDescription = ErrDesc.Trim
        End Get
    End Property

    Protected Overrides Sub Finalize()
        Mail.Dispose()
        MyBase.Finalize()
    End Sub

    Public Property From() As String
        Get
            From = sFrom.Trim
        End Get
        Set(ByVal value As String)
            If value.Contains("@") = True And value.Contains(".") = True Then
                sFrom = value.Trim
                mFrom = New MailAddress(value.Trim)
            Else
                ErrNum = -1
                ErrDesc = "Invalid email address given."
            End If
        End Set
    End Property

    Public Property HostServer() As String
        Get
            HostServer = sHostServer.Trim
        End Get
        Set(ByVal value As String)
            sHostServer = value.Trim
        End Set
    End Property

    Public Sub New()
        Mail = New MailMessage
        Mail.To.Clear()
        Mail.CC.Clear()
        Mail.Bcc.Clear()
        Mail.Attachments.Clear()
    End Sub

    Public Property Priority() As MailPriority
        Get
            Priority = Mail.Priority
        End Get
        Set(ByVal value As MailPriority)
            Mail.Priority = value
        End Set
    End Property

    Public Sub Send()
        Try
            Mail.From = mFrom
            Mail.BodyEncoding = System.Text.Encoding.Default
            Mail.IsBodyHtml = True
            'Smtpclient to send the mail message
            Dim SmtpMail As New SmtpClient
            SmtpMail.Host = sHostServer
            SmtpMail.Send(Mail)
        Catch ex As Exception
            ErrNum = -1
            ErrDesc = "Failure to send email. Verify addresses and attachments."
            ErrDesc = ex.Message
        End Try
    End Sub

    Public Property sTo()
        Get
            sTo = Mail.To.ToString
        End Get
        Set(ByVal value)
            If value.Contains("@") And value.Contains(".") Then
                Mail.To.Add(value.Trim)
            Else
                ErrNum = -1
                ErrDesc = "Invalid email address given."
            End If
        End Set
    End Property

    Public Property Subject() As String
        Get
            Subject = Mail.Subject
        End Get
        Set(ByVal value As String)
            If value.Trim.Length = 0 Then
                Mail.Subject = ""
            Else
                Mail.Subject = value
            End If
        End Set
    End Property
End Class

"...and did we give up when the Germans bombed Pearl Harbor? NO!"
"Don't stop him. He's roll'n.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top