Results 1 to 5 of 5

Thread: sending outlook email

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Resolved sending outlook email

    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:
    1. Imports Outlook
    2.  
    3. Public Class Email
    4.     Private mOutLookApp As Outlook.Application
    5.     Private mNameSpace As Outlook.NameSpace
    6.     Private mItem As Outlook.MailItem
    7.     Private strAttach As String
    8.  
    9.     Public Event emailUpdate(ByVal status As String, ByVal code As Integer)
    10.     Public Event emailSubject(ByVal status As String, ByVal cod As Integer)
    11.  
    12.     'mNameSpace.Logon(, , False, True)
    13.  
    14.     Private mMailSubject As String
    15.     Private mMailTo As String
    16.     Private mMailAttachment As String
    17.  
    18.  
    19.  
    20.     Public Sub New()
    21.  
    22.         mOutLookApp = New Outlook.Application
    23.         mNameSpace = mOutLookApp.GetNamespace("MAPI")
    24.        
    25.     End Sub
    26.  
    27.  
    28.  
    29.     Public Property MailSubject() As String
    30.         Get
    31.             Return mMailSubject
    32.         End Get
    33.         Set(ByVal MailSubject As String)
    34.            
    35.             mMailSubject = MailSubject
    36.         End Set
    37.     End Property
    38.     Public Property MailTo() As String
    39.         Get
    40.             Return MailTo
    41.         End Get
    42.         Set(ByVal MailTo As String)
    43.            
    44.             mMailTo = MailTo
    45.         End Set
    46.     End Property
    47.     Public Property MailAttachment() As String
    48.         Get
    49.             Return MailAttachment
    50.         End Get
    51.         Set(ByVal MailAttachment As String)
    52.            
    53.             mMailAttachment = MailAttachment
    54.         End Set
    55.     End Property
    56.  
    57.     Public Function SendAttachment() As Boolean
    58.         If mMailAttachment <> "" Then
    59.             'send attachment
    60.             Return True
    61.         End If
    62.     End Function
    63.  
    64.  
    65.  
    66.     Public Function SendMail() As Integer
    67.         Try
    68.             If mMailTo = "" Then
    69.                 RaiseEvent emailUpdate("mail to address is blank", -125)
    70.                 Return -1
    71.             Else
    72.             End If
    73.             If mMailSubject = "" Then
    74.                 RaiseEvent emailSubject("mail subject is blank", -135)
    75.                 Return -1
    76.             Else
    77.             End If
    78.             If mMailAttachment = "" Then
    79.                 mItem.Send()
    80.             End If
    81.  
    82.  
    83.             Dim t As New System.Threading.Thread(AddressOf SendMailThread)
    84.             t.Start()
    85.             Return 1
    86.         Catch ex As System.Runtime.InteropServices.COMException
    87.             Return -1
    88.  
    89.         End Try
    90.  
    91.  
    92.     End Function
    93.  
    94.     Private Sub SendMailThread()
    95.  
    96.         Try
    97.             mItem = mOutLookApp.CreateItem(0)
    98.             mItem.To = mMailTo
    99.             mItem.Subject = mMailSubject
    100.             If mItem.Attachments.Add(mMailAttachment) Is Nothing Then
    101.                 mItem.Send()
    102.                 System.Windows.Forms.MessageBox.Show("success, email  sent")
    103.             Else
    104.  
    105.                 mItem.Attachments.Add(mMailAttachment)
    106.                 mItem.Send()
    107.                 System.Windows.Forms.MessageBox.Show("success, email and attachment sent")
    108.             End If
    109.  
    110.         Catch ex As System.Runtime.InteropServices.COMException
    111.             MsgBox("error mail not sent")
    112.  
    113.         End Try
    114.  
    115.     End Sub
    116.  
    117. End Class


    Here is the code in my test app. All my test app is a form with a button.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         gobjemail.MailTo = txtTo.Text
    3.         gobjemail.MailSubject = txtSubject.Text
    4.         gobjemail.MailAttachment = txtAttachment.Text
    5.         gobjemail.SendMail()
    6.        
    7.     End Sub
    8.  
    9.     Private Sub gobjemail_emailUpdate(ByVal status As String, ByVal code As Integer) Handles gobjemail.emailUpdate
    10.         MsgBox("email update")
    11.     End Sub
    12.  
    13.     Private Sub gobjemail_emailSubject(ByVal status As String, ByVal code As Integer) Handles gobjemail.emailUpdate
    14.         MsgBox("email subject")
    15.     End Sub
    16.  
    17. End Class
    Last edited by miry2j; Apr 30th, 2005 at 04:44 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending outlook email

    Welcome to the Forums.

    In your attachment property you are returning True for an attachment presence, but not
    returning False when it doesnt exist. Also you may be better off comparing to String.Empty instead of "".
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    Re: sending outlook email

    Thanks for your response but I finally figured out a way past the with and without attachment. What I did was make another sub that had attachment stuff in it. In my tester I had an if statement
    if txtAttachment = "" then
    call the sub with no attachment
    else if txtAttachment <> "" then
    call the sub with the attachment in it
    end if.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending outlook email

    You should also do it in the property too since if no attachment is found then it should return False. Then when you
    handle it in code you use the same logic from the property to determine.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: sending outlook email

    I noticed the last two event handlers in your test code are handling the same event. Is that a typo?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width