I am trying to write a dll to send email with a hook to outlook.
The problem I am having is with the attachments. In my test app if I put in the To, Subject and the attachment. It will send me the email. If I leave out the attachment in the test app I get an exception null reference. Not sure how to go about sending the email whether it has the attachment or not.
Any help would be appreciated. Here is my code for my email class:
VB Code:
Imports Outlook Public Class Email Private mOutLookApp As Outlook.Application Private mNameSpace As Outlook.NameSpace Private mItem As Outlook.MailItem Private strAttach As String Public Event emailUpdate(ByVal status As String, ByVal code As Integer) Public Event emailSubject(ByVal status As String, ByVal cod As Integer) 'mNameSpace.Logon(, , False, True) Private mMailSubject As String Private mMailTo As String Private mMailAttachment As String Public Sub New() mOutLookApp = New Outlook.Application mNameSpace = mOutLookApp.GetNamespace("MAPI") End Sub 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 Public Function SendAttachment() As Boolean If mMailAttachment <> "" Then 'send attachment Return True End If End Function Public Function SendMail() As Integer Try If mMailTo = "" Then RaiseEvent emailUpdate("mail to address is blank", -125) Return -1 Else End If If mMailSubject = "" Then RaiseEvent emailSubject("mail subject is blank", -135) Return -1 Else End If If mMailAttachment = "" Then mItem.Send() End If Dim t As New System.Threading.Thread(AddressOf SendMailThread) t.Start() Return 1 Catch ex As System.Runtime.InteropServices.COMException Return -1 End Try End Function Private Sub SendMailThread() Try mItem = mOutLookApp.CreateItem(0) mItem.To = mMailTo mItem.Subject = mMailSubject If mItem.Attachments.Add(mMailAttachment) Is Nothing Then mItem.Send() System.Windows.Forms.MessageBox.Show("success, email sent") Else mItem.Attachments.Add(mMailAttachment) mItem.Send() System.Windows.Forms.MessageBox.Show("success, email and attachment sent") End If Catch ex As System.Runtime.InteropServices.COMException MsgBox("error mail not sent") End Try End Sub End Class
Here is the code in my test app. All my test app is a form with a button.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click gobjemail.MailTo = txtTo.Text gobjemail.MailSubject = txtSubject.Text gobjemail.MailAttachment = txtAttachment.Text gobjemail.SendMail() End Sub Private Sub gobjemail_emailUpdate(ByVal status As String, ByVal code As Integer) Handles gobjemail.emailUpdate MsgBox("email update") End Sub Private Sub gobjemail_emailSubject(ByVal status As String, ByVal code As Integer) Handles gobjemail.emailUpdate MsgBox("email subject") End Sub End Class
