Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

VB.Net: Sending a mail with attachements

From Wiki

Jump to: navigation, search

this is a draft version.

  1. Imports System.Net.Mail
  2.  
  3. Public Class SendMail
  4.  
  5. #Region " Private members "
  6.     Private _sender As String
  7.     Private _recipient As String
  8.     Private _subject As String
  9.     Private _sMTPServer As String
  10.     Private _sMTPUser As String
  11.     Private _sMTPPassword As String
  12.     Private _logFileNames As String
  13.     Private _fileNames() As String
  14.     Private _Log As TDB2007.Utils.Logging.Logging.Log
  15. #End Region
  16.  
  17. #Region " Constructors "
  18.     Public Sub New()
  19.         _Log = TDB2007.Utils.Logging.Logging.Log.Instance
  20.         Dim _MailSettings As New MailSettings
  21.         Sender = _MailSettings.Sender
  22.         Recipient = _MailSettings.Recipient
  23.         Subject = _MailSettings.Subject
  24.         SMTPServer = _MailSettings.SMTPServer
  25.         SMTPPassword = _MailSettings.SMTPPassword
  26.         LogFileNames = _MailSettings.LogFilenames
  27.     End Sub
  28. #End Region
  29.  
  30. #Region " Public properties "
  31.     Public Property Sender() As String
  32.         Get
  33.             Return _sender
  34.         End Get
  35.         Set(ByVal value As String)
  36.             _sender = value
  37.         End Set
  38.     End Property
  39.  
  40.     Public Property Recipient() As String
  41.         Get
  42.             Return _recipient
  43.         End Get
  44.         Set(ByVal value As String)
  45.             _recipient = value
  46.         End Set
  47.     End Property
  48.  
  49.     Public Property Subject() As String
  50.         Get
  51.             Return _subject
  52.         End Get
  53.         Set(ByVal value As String)
  54.             _subject = value
  55.         End Set
  56.     End Property
  57.  
  58.     Public Property SMTPServer() As String
  59.         Get
  60.             Return _sMTPServer
  61.         End Get
  62.         Set(ByVal value As String)
  63.             _sMTPServer = value
  64.         End Set
  65.     End Property
  66.  
  67.     Public Property SMTPUser() As String
  68.         Get
  69.             Return _sMTPUser
  70.         End Get
  71.         Set(ByVal value As String)
  72.             _sMTPUser = value
  73.         End Set
  74.     End Property
  75.  
  76.     Public Property SMTPPassword() As String
  77.         Get
  78.             Return _sMTPPassword
  79.         End Get
  80.         Set(ByVal value As String)
  81.             _sMTPPassword = value
  82.         End Set
  83.     End Property
  84.  
  85.     Public Property LogFileNames() As String
  86.         Get
  87.             Return _logFileNames
  88.         End Get
  89.         Set(ByVal value As String)
  90.             _logFileNames = value
  91.             _fileNames = _logFileNames.Split(New String() {";"}, StringSplitOptions.None)
  92.         End Set
  93.     End Property
  94.  
  95.     Public ReadOnly Property Filenames() As String()
  96.         Get
  97.             Return _fileNames
  98.         End Get
  99.     End Property
  100. #End Region
  101.  
  102. #Region " Public methods "
  103.     Public Sub Send()
  104.         Dim _Thread As New Threading.Thread(AddressOf Me.SendThread)
  105.         _Thread.Start()
  106.     End Sub
  107.  
  108.     Private Sub SendThread()
  109.         Try
  110.             _subject = String.Format(_subject, Environment.UserName, Environment.MachineName)
  111.             Dim _message As MailMessage = New MailMessage(_sender, _recipient, _subject, "")
  112.             _message.IsBodyHtml = True
  113.             For Each _Filename As String In _fileNames
  114.                 Try
  115.                     If System.IO.File.Exists(_Filename) Then
  116.                         System.IO.File.Delete(_Filename)
  117.                     End If
  118.                     System.IO.File.Copy(_Filename, _Filename)
  119.                     Dim _attachment As New System.Net.Mail.Attachment(_Filename)
  120.                     _message.Attachments.Add(_attachment)
  121.                 Catch ex As Exception
  122.                     _Log.LogWarning("Send - Error: " & ex.Message)
  123.                 End Try
  124.             Next
  125.             Dim _mailClient As SmtpClient = New SmtpClient()
  126.             _mailClient.Host = _sMTPServer
  127.             _mailClient.Credentials = New System.Net.NetworkCredential(_sMTPUser, _sMTPPassword)
  128.             _mailClient.UseDefaultCredentials = False
  129.             _mailClient.DeliveryMethod = SmtpDeliveryMethod.Network
  130.             _mailClient.Send(_message)
  131.         Catch ex As SmtpException
  132.             _Log.LogWarning("Send - Error: " & ex.Message)
  133.         Catch ex As Exception
  134.             _Log.LogWarning("Send - Error: " & ex.Message)
  135.         End Try
  136.     End Sub
  137. #End Region
  138.  
  139. End Class

389 Rating: 1.3/5 (3 votes cast)