Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
VB.Net: Sending a mail with attachements
From Wiki
this is a draft version.
- Imports System.Net.Mail
- Public Class SendMail
- #Region " Private members "
- Private _sender As String
- Private _recipient As String
- Private _subject As String
- Private _sMTPServer As String
- Private _sMTPUser As String
- Private _sMTPPassword As String
- Private _logFileNames As String
- Private _fileNames() As String
- Private _Log As TDB2007.Utils.Logging.Logging.Log
- #End Region
- #Region " Constructors "
- Public Sub New()
- _Log = TDB2007.Utils.Logging.Logging.Log.Instance
- Dim _MailSettings As New MailSettings
- Sender = _MailSettings.Sender
- Recipient = _MailSettings.Recipient
- Subject = _MailSettings.Subject
- SMTPServer = _MailSettings.SMTPServer
- SMTPPassword = _MailSettings.SMTPPassword
- LogFileNames = _MailSettings.LogFilenames
- End Sub
- #End Region
- #Region " Public properties "
- Public Property Sender() As String
- Get
- Return _sender
- End Get
- Set(ByVal value As String)
- _sender = value
- End Set
- End Property
- Public Property Recipient() As String
- Get
- Return _recipient
- End Get
- Set(ByVal value As String)
- _recipient = value
- End Set
- End Property
- Public Property Subject() As String
- Get
- Return _subject
- End Get
- Set(ByVal value As String)
- _subject = value
- End Set
- End Property
- Public Property SMTPServer() As String
- Get
- Return _sMTPServer
- End Get
- Set(ByVal value As String)
- _sMTPServer = value
- End Set
- End Property
- Public Property SMTPUser() As String
- Get
- Return _sMTPUser
- End Get
- Set(ByVal value As String)
- _sMTPUser = value
- End Set
- End Property
- Public Property SMTPPassword() As String
- Get
- Return _sMTPPassword
- End Get
- Set(ByVal value As String)
- _sMTPPassword = value
- End Set
- End Property
- Public Property LogFileNames() As String
- Get
- Return _logFileNames
- End Get
- Set(ByVal value As String)
- _logFileNames = value
- _fileNames = _logFileNames.Split(New String() {";"}, StringSplitOptions.None)
- End Set
- End Property
- Public ReadOnly Property Filenames() As String()
- Get
- Return _fileNames
- End Get
- End Property
- #End Region
- #Region " Public methods "
- Public Sub Send()
- Dim _Thread As New Threading.Thread(AddressOf Me.SendThread)
- _Thread.Start()
- End Sub
- Private Sub SendThread()
- Try
- _subject = String.Format(_subject, Environment.UserName, Environment.MachineName)
- Dim _message As MailMessage = New MailMessage(_sender, _recipient, _subject, "")
- _message.IsBodyHtml = True
- For Each _Filename As String In _fileNames
- Try
- If System.IO.File.Exists(_Filename) Then
- System.IO.File.Delete(_Filename)
- End If
- System.IO.File.Copy(_Filename, _Filename)
- Dim _attachment As New System.Net.Mail.Attachment(_Filename)
- _message.Attachments.Add(_attachment)
- Catch ex As Exception
- _Log.LogWarning("Send - Error: " & ex.Message)
- End Try
- Next
- Dim _mailClient As SmtpClient = New SmtpClient()
- _mailClient.Host = _sMTPServer
- _mailClient.Credentials = New System.Net.NetworkCredential(_sMTPUser, _sMTPPassword)
- _mailClient.UseDefaultCredentials = False
- _mailClient.DeliveryMethod = SmtpDeliveryMethod.Network
- _mailClient.Send(_message)
- Catch ex As SmtpException
- _Log.LogWarning("Send - Error: " & ex.Message)
- Catch ex As Exception
- _Log.LogWarning("Send - Error: " & ex.Message)
- End Try
- End Sub
- #End Region
- End Class



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.