I am writing a dll to send email with or without attachments. I am trying to validate the attachment. If the size of the attachment is greater than 1mb I want to fire an event and return -1. I have tried this piece of code to try and validate but cannot get it to work. It still sends the attachment. By the way the attachments I am trying to send are the first one's size 1.05 mb the other one is 6.09 mb.

Here is the code:

VB Code:
  1. Imports Outlook
  2. Imports System.IO
  3. Public Class Email
  4.     Private mOutLookApp As Outlook.Application
  5.     Private mNameSpace As Outlook.NameSpace
  6.     Private mItem As Outlook.MailItem
  7.     Private mAttachment As Outlook.Attachment
  8.     Private strAttach As String
  9.  
  10.     Public Event emailUpdate(ByVal status As String, ByVal code As Integer)
  11.  
  12.  
  13.  
  14.  
  15.     Private mMailSubject As String
  16.     Private mMailTo As String
  17.     Private mMailAttachment As String
  18.  
  19.  
  20.     ' constructor for outlook instance
  21.     Public Sub New()
  22.         Try
  23.             mOutLookApp = New Outlook.Application
  24.             mNameSpace = mOutLookApp.GetNamespace("MAPI")
  25.  
  26.         Catch ex As System.Runtime.InteropServices.COMException
  27.  
  28.         End Try
  29.        
  30.        
  31.     End Sub
  32.  
  33.  
  34.     'Properties for mail subject, mail to, and mail attachment
  35.     Public Property MailSubject() As String
  36.         Get
  37.             Return mMailSubject
  38.         End Get
  39.         Set(ByVal MailSubject As String)
  40.            
  41.             mMailSubject = MailSubject
  42.         End Set
  43.     End Property
  44.     Public Property MailTo() As String
  45.         Get
  46.             Return MailTo
  47.         End Get
  48.         Set(ByVal MailTo As String)
  49.            
  50.             mMailTo = MailTo
  51.         End Set
  52.     End Property
  53.     Public Property MailAttachment() As String
  54.         Get
  55.             Return MailAttachment
  56.         End Get
  57.         Set(ByVal MailAttachment As String)
  58.            
  59.             mMailAttachment = MailAttachment
  60.         End Set
  61.     End Property
  62.  
  63.     'Thread for sending mail with no attachment with events
  64.     Public Function SendMailNo() As Integer
  65.         Try
  66.             If mMailTo = "" Then
  67.                 RaiseEvent emailUpdate("mail to address is blank", -125)
  68.                 Return -1
  69.                 ' Else
  70.                 ' End If
  71.             ElseIf mMailSubject = "" Then
  72.                 RaiseEvent emailUpdate("mail subject is blank", -135)
  73.                 Return -1
  74.                 ' Else
  75.             End If
  76.  
  77.             Dim t As New System.Threading.Thread(AddressOf SendMailThreadNoAtt)
  78.             t.Start()
  79.             Return 1
  80.         Catch ex As System.Runtime.InteropServices.COMException
  81.             Return -1
  82.  
  83.         End Try
  84.        
  85.  
  86.     End Function
  87.     'Thread for sending mail with attachment
  88.     Public Function SendMailAtt() As Integer
  89.         Dim MAX_LENGTH As Integer
  90.         MAX_LENGTH = 1048576     <--------- Here is where I declare 1 mb
  91.         Try
  92.  
  93.             If Dir(mMailAttachment, vbNormal) <> "" Then
  94.                 If FileLen(mMailAttachment) > MAX_LENGTH Then    <-- valdiation
  95.                     MsgBox(FileLen(mMailAttachment) / 1024 & " kbytes")
  96.                     MsgBox(MAX_LENGTH & " kbytes")
  97.                     RaiseEvent emailUpdate("file att larger than 1mb", -175)
  98.                     ' MsgBox("file too big")
  99.                     Return -1
  100.                 End If
  101.                 'MsgBox("file exists")
  102.                 RaiseEvent emailUpdate("file attachment exists", 300)
  103.  
  104.  
  105.             Else
  106.                 ' MsgBox("file does not exist")
  107.                 RaiseEvent emailUpdate("file att does not exist", -165)
  108.                 Return -1
  109.             End If
  110.  
  111.             'If FileLen(mMailAttachment) > 20000000 Then
  112.             ' MsgBox(FileLen(mMailAttachment) / 1024 & " kbytes")
  113.             ' MsgBox(MAX_LENGTH & " kbytes")
  114.  
  115.             Dim t As New System.Threading.Thread(AddressOf SendMailThreadAtt)
  116.             t.Start()
  117.             Return 1
  118.         Catch ex As System.Runtime.InteropServices.COMException
  119.             Return -1
  120.  
  121.         End Try
  122.  
  123.  
  124.     End Function
  125.  
  126.     'Sub to send mail with no attachment
  127.     Private Sub SendMailThreadNoAtt()
  128.         Dim sSource As String
  129.         Try
  130.             mItem = mOutLookApp.CreateItem(0)
  131.             mItem.To = mMailTo
  132.             mItem.Subject = mMailSubject
  133.             mItem.Send()
  134.             RaiseEvent emailUpdate("mail with no att sent", 200)
  135.  
  136.  
  137.         Catch ex As System.Runtime.InteropServices.COMException
  138.             RaiseEvent emailUpdate("error sending mail", -145)
  139.  
  140.  
  141.         End Try
  142.  
  143.     End Sub
  144.  
  145.     'Sub to send mail with attachment
  146.     Private Sub SendMailThreadAtt()
  147.         Dim sSource As String
  148.         Dim size As Integer
  149.         Try
  150.  
  151.             mItem = mOutLookApp.CreateItem(0)
  152.  
  153.             mItem.To = mMailTo
  154.             mItem.Subject = mMailSubject
  155.  
  156.             mItem.Attachments.Add(mMailAttachment)
  157.             'MsgBox(FileLen("c:\Intern\test.txt") & "bytes")
  158.  
  159.             mItem.Send()
  160.             RaiseEvent emailUpdate("mail with att sent", 100)
  161.  
  162.  
  163.  
  164.         Catch ex As System.Runtime.InteropServices.COMException
  165.             RaiseEvent emailUpdate("error sending mail with att", -155)
  166.  
  167.  
  168.         End Try
  169.  
  170.     End Sub
  171.  
  172.  
  173. End Class