Imports Outlook
Imports System.IO
Public Class Email
Private mOutLookApp As Outlook.Application
Private mNameSpace As Outlook.NameSpace
Private mItem As Outlook.MailItem
Private mAttachment As Outlook.Attachment
Private strAttach As String
Public Event emailUpdate(ByVal status As String, ByVal code As Integer)
Private mMailSubject As String
Private mMailTo As String
Private mMailAttachment As String
' constructor for outlook instance
Public Sub New()
Try
mOutLookApp = New Outlook.Application
mNameSpace = mOutLookApp.GetNamespace("MAPI")
Catch ex As System.Runtime.InteropServices.COMException
End Try
End Sub
'Properties for mail subject, mail to, and mail attachment
Public Property MailSubject() As String
Get
Return mMailSubject
End Get
Set(ByVal MailSubject As String)
mMailSubject = MailSubject
End Set
End Property
Public Property MailTo() As String
Get
Return MailTo
End Get
Set(ByVal MailTo As String)
mMailTo = MailTo
End Set
End Property
Public Property MailAttachment() As String
Get
Return MailAttachment
End Get
Set(ByVal MailAttachment As String)
mMailAttachment = MailAttachment
End Set
End Property
'Thread for sending mail with no attachment with events
Public Function SendMailNo() As Integer
Try
If mMailTo = "" Then
RaiseEvent emailUpdate("mail to address is blank", -125)
Return -1
' Else
' End If
ElseIf mMailSubject = "" Then
RaiseEvent emailUpdate("mail subject is blank", -135)
Return -1
' Else
End If
Dim t As New System.Threading.Thread(AddressOf SendMailThreadNoAtt)
t.Start()
Return 1
Catch ex As System.Runtime.InteropServices.COMException
Return -1
End Try
End Function
'Thread for sending mail with attachment
Public Function SendMailAtt() As Integer
Dim MAX_LENGTH As Integer
MAX_LENGTH = 1048576 <--------- Here is where I declare 1 mb
Try
If Dir(mMailAttachment, vbNormal) <> "" Then
If FileLen(mMailAttachment) > MAX_LENGTH Then <-- valdiation
MsgBox(FileLen(mMailAttachment) / 1024 & " kbytes")
MsgBox(MAX_LENGTH & " kbytes")
RaiseEvent emailUpdate("file att larger than 1mb", -175)
' MsgBox("file too big")
Return -1
End If
'MsgBox("file exists")
RaiseEvent emailUpdate("file attachment exists", 300)
Else
' MsgBox("file does not exist")
RaiseEvent emailUpdate("file att does not exist", -165)
Return -1
End If
'If FileLen(mMailAttachment) > 20000000 Then
' MsgBox(FileLen(mMailAttachment) / 1024 & " kbytes")
' MsgBox(MAX_LENGTH & " kbytes")
Dim t As New System.Threading.Thread(AddressOf SendMailThreadAtt)
t.Start()
Return 1
Catch ex As System.Runtime.InteropServices.COMException
Return -1
End Try
End Function
'Sub to send mail with no attachment
Private Sub SendMailThreadNoAtt()
Dim sSource As String
Try
mItem = mOutLookApp.CreateItem(0)
mItem.To = mMailTo
mItem.Subject = mMailSubject
mItem.Send()
RaiseEvent emailUpdate("mail with no att sent", 200)
Catch ex As System.Runtime.InteropServices.COMException
RaiseEvent emailUpdate("error sending mail", -145)
End Try
End Sub
'Sub to send mail with attachment
Private Sub SendMailThreadAtt()
Dim sSource As String
Dim size As Integer
Try
mItem = mOutLookApp.CreateItem(0)
mItem.To = mMailTo
mItem.Subject = mMailSubject
mItem.Attachments.Add(mMailAttachment)
'MsgBox(FileLen("c:\Intern\test.txt") & "bytes")
mItem.Send()
RaiseEvent emailUpdate("mail with att sent", 100)
Catch ex As System.Runtime.InteropServices.COMException
RaiseEvent emailUpdate("error sending mail with att", -155)
End Try
End Sub
End Class