|
-
Apr 30th, 2005, 09:36 AM
#1
Thread Starter
New Member
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:
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
Last edited by miry2j; Apr 30th, 2005 at 04:44 PM.
-
Apr 30th, 2005, 11:32 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 30th, 2005, 04:47 PM
#3
Thread Starter
New Member
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.
-
May 1st, 2005, 10:50 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 8th, 2005, 02:43 PM
#5
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|