These are the same classes in VB.NET in case needed.
I've also made the mail server address a parameter to allow the code to be runnable on copy paste.
The problem while sending the bigger files was the smtp client's timeout(default 100s). I've made this dependent on the attachments' size allowing higher timeouts for bigger files.
Imports System.Text
Imports System.Net.Mail
Public Class SMTPMailSender
Public Shared Sub Email(ByVal mailServerAddress As String, ByVal [to] As String, ByVal body As String, ByVal subject As String, ByVal fromAddress As String, ByVal fromDisplay As String, ByVal credentialUser As String, ByVal credentialPassword As String, ByVal attachments() As MailAttachment)
Dim host As String = mailServerAddress
body = UpgradeEmailFormat(body)
Try
Dim mail As MailMessage = New MailMessage()
mail.Body = body
mail.IsBodyHtml = True
mail.To.Add(New MailAddress([to]))
mail.From = New MailAddress(fromAddress, fromDisplay, Encoding.UTF8)
mail.Subject = subject
mail.SubjectEncoding = Encoding.UTF8
mail.Priority = MailPriority.Normal
For Each ma As MailAttachment In attachments
mail.Attachments.Add(ma.File)
Next
Dim Smtp As SmtpClient = New SmtpClient
Smtp.EnableSsl = True
Smtp.Credentials = New System.Net.NetworkCredential(credentialUser, credentialPassword)
Smtp.Host = host
Smtp.Timeout = Math.Max(attachments.Sum(Function(Item) (DirectCast(Item, MailAttachment).Size / 1024)), 100) * 1000
Smtp.Send(mail)
Catch
Dim sb As New StringBuilder(1024)
sb.Append("\nTo:" + [to])
sb.Append("\nbody:" + body)
sb.Append("\nsubject:" + subject)
sb.Append("\nfromAddress:" + fromAddress)
sb.Append("\nfromDisplay:" + fromDisplay)
sb.Append("\ncredentialUser:" + credentialUser)
sb.Append("\ncredentialPasswordto:" + credentialPassword)
sb.Append("\nHosting:" + host)
Debug.Print(sb.ToString)
End Try
End Sub
Private Shared Function UpgradeEmailFormat(ByVal body As String) As String
Return body
End Function
End Class
The MailAttachment
Class has a new property size that is used to determine the attachment data length:
Imports System.IO
Imports System.Net.Mail
Imports System.Net.Mime
Public Class MailAttachment
Private _stream As MemoryStream
Public Property Stream() As MemoryStream
Get
Return _stream
End Get
Set(ByVal value As MemoryStream)
_stream = value
End Set
End Property
Private _filename As String
Public Property Filename() As String
Get
Return _filename
End Get
Set(ByVal value As String)
_filename = value
End Set
End Property
Private _mediaType As String
Public Property MediaType() As String
Get
Return _mediaType
End Get
Set(ByVal value As String)
_mediaType = value
End Set
End Property
Public ReadOnly Property File() As Attachment
Get
Return New Attachment(Stream, Filename, MediaType)
End Get
End Property
Public ReadOnly Property Size() As Double
Get
Return Me.Stream.Length
End Get
End Property
Public Sub New(ByVal data() As Byte, ByVal filename As String)
Me.Stream = New MemoryStream(data)
Me.Filename = filename
Me.MediaType = MediaTypeNames.Application.Octet
End Sub
Public Sub New(ByVal data As String, ByVal filename As String)
Me.Stream = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(data))
Me.Filename = filename
Me.MediaType = MediaTypeNames.Text.Html
End Sub
End Class
Hariharan leapt into the field of programming after being introduced to C in his second year at college. A fan of basketball and chess, the logical thought process behind programming and the concepts of linking real principles onto a concrete platform pushed him deep into this field. On finishing his bachelor's majoring in Electrical and Electronics engineering with Soft Computing, Numerical Analysis and Biomedical Engineering as minor, he did a six month stint at India's second largest IT company- Infosys Technologies. He left Infosys armed with strong concepts of SDL Cycles and process development, gained domain knowledge in Java and had explored Visual basic, C++, C# and kept his mind open for more. Currently working in a startup as the product designer, his arsenal of technologies has doubled to accommodate the challenges his job demands.
Off work, he enjoys learning new languages (non-programming) and is currently teaching himself German and Spanish. He also enjoys travelling around and exploring new ideas, places and relationships.
Currently attempting to extend his education to post graduation.