Cannot Send Mail Through Outlook without Outlook Open

  • Thread starter Thread starter Barkster
  • Start date Start date
B

Barkster

I'm trying to send using outlook but I get an error on the directcast line
if I don't have outlook open. Works fine if it is open. What do I need to
do to be able to send without having outlook open? Thanks

Dim objOutlook As New Outlook.Application

Dim objMail As Outlook._MailItem =
DirectCast(objOutlook.CreateItem(Outlook.OlItemType.olMailItem),
Outlook._MailItem)

Dim mSubject As String

Dim mBody As String

mSubject = client & " - " & fDescription.Text

mBody = ControlChars.CrLf & "file:" & fFilename.Text & ControlChars.CrLf &
ControlChars.CrLf &

With objMail

..BodyFormat = Outlook.OlBodyFormat.olFormatRichText

..To = (e-mail address removed)

..Subject = mSubject

..Body = mBody

..Send()

End With

objMail = Nothing

objOutlook = Nothing
 
¤ I'm trying to send using outlook but I get an error on the directcast line
¤ if I don't have outlook open. Works fine if it is open. What do I need to
¤ do to be able to send without having outlook open? Thanks
¤
¤ Dim objOutlook As New Outlook.Application
¤

Try the following instead of the above line of code:

Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Tried that and got the same error, stops on line
Dim objMail As Outlook._MailItem =
DirectCast(objOutlook.CreateItem(Outlook.OlItemType.olMailItem),
Outlook._MailItem)


with the following error

An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in ScanMail.exe



B-Dog said:
will do, thanks

 
¤ Tried that and got the same error, stops on line
¤ Dim objMail As Outlook._MailItem =
¤ DirectCast(objOutlook.CreateItem(Outlook.OlItemType.olMailItem),
¤ Outlook._MailItem)
¤
¤
¤ with the following error
¤
¤ An unhandled exception of type 'System.Runtime.InteropServices.COMException'
¤ occurred in ScanMail.exe

What is ScanMail? Is it anti-virus software for Exchange?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Figured it out, wasn't logining in using using mapi and profile

Try

'now need to send to Jack from list

Dim objOutlook As Outlook._Application



Dim OLNameSpace As Outlook.NameSpace

Dim objMail As Outlook.MailItem

Dim mSubject As String

Dim mBody As String

objOutlook = New Outlook.Application

OLNameSpace = objOutlook.GetNamespace("MAPI")

OLNameSpace.Logon()

objMail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem),
Outlook.MailItem)

mBody = ""

mSubject = "Daily Attendance"

Dim icount As Integer

For icount = 0 To lbAttend.Items.Count - 1

mBody &= CType(CType(lbAttend.Items(icount), DataRowView)("Description"),
String) & ControlChars.CrLf

Next

With objMail

..BodyFormat = Outlook.OlBodyFormat.olFormatRichText

..To = "(e-mail address removed)"

..Subject = mSubject

..Body = mBody

..Send()

End With

OLNameSpace.Logoff()

OLNameSpace = Nothing

objMail = Nothing

objOutlook = Nothing

Catch ex As Exception

MsgBox(ex)

End Try

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top