Error receiving email

NJDubois

Well-known member
Joined
May 15, 2008
Messages
84
Programming Experience
Beginner
So I'm setting up my mailing list program. Everything is going and working great. It catches the message from outlook when a new message is received.

But I can't do anything with the data? The code for the message catch is

VB.NET:
Dim OutlookNS As Outlook.NameSpace
OutlookNS = outlookApp.GetNamespace("MAPI")

Dim Inbox As Outlook.MAPIFolder
Inbox = OutlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

Dim NewMail As Outlook.MailItem
NewMail = OutlookNS.GetItemFromID(EntryIDCollection, Inbox.StoreID)

Dim str As String = NewMail.SenderEmailAddress & "," & NewMail.Subject
but I can't do anything with str?
for example
msgbox(str) works great! I get a pretty message box saying "whoever@whatever.com, subscribe"

BUT, if I try to do anything else with it
VB.NET:
textbox1.text = str
I get this error

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Why is this happening?

Thanks for the help guys. I'll be a vb.net programmer someday...

Nick
 
That's weird... If the String can be sent to a message box, it is correct... Have you tried replacing <textbox1.text = str> by <textbox1.text = "abc">?

Maybe the TextBox or the form it is on are disposed or something...
 
On the Debug menu, Click Exceptions* and put a tick next to "thrown"

Show us the exact line of code that is throwing this error if possible

*you might have to customize your menu for this (right click any toolbar and click Customize), as some vb people seem not to have it by default
 
update...

Ok, the line that seems to give me the error is anytime I try to use the data.

textbox1.text = str

or

listbox1.items.add(str)

you name it, it does it. It doesn't kill the program, and everytime that line of code is ran it outputs the error again.

but if I do say textbox1.text = "Hello"
or the same for the listbox in place of the str it works as it is supposed to.

When I get home I will look to see if Exceptions is set to "thrown", but I am pretty sure it isn't.

Thanks for the replys. I hope that exceptions is set wrong, cause this is driving me crazy!

Thanks for the fast replys, and if there is anything else I can do please post!

Thanks
nick
 
Can you give us the stack trace of the exception to see if you don't have some validation, format or anything that can be caused by the textbox's inner working?

I must admit I have no idea... Can you post some code? I mean some structure so we can look for anything that might lie out of the few lines you already posted...
 
also switch on option explicit and option strict, fix any compiler errors that doing so realises
 
The source.

Because I have a lot of things on my todo list and today is not a day for coding I do not have any time to mess with this right now. Here is the source code, I hope it helps you to help me!

I followed this howto
http://www.devx.com/dotnet/Article/35900

The code I am giving you is a simplified version of it. Just a listbox, and the code needed to catch the email.

I regret to say that you need to have outlook (outlook express wouldn't work?) in order to test it, and get the code where I am having the problem used.

Thanks for the aid!

Nick
 

Attachments

  • WhyNotMailingList.zip
    15 KB · Views: 28
There is attempt to access UI from different thread, perhaps because it is a COM object there is no exception message for it. Using the standard invoke pattern solves it:
VB.NET:
Private Delegate Sub SetTextHandler(ByVal txt As String)

Private Sub SetText(ByVal txt As String)
    If Me.ListBox1.InvokeRequired Then
        Dim d As New SetTextHandler(AddressOf SetText)
        Me.ListBox1.Invoke(d, txt)
    Else
        Me.ListBox1.Items.Add(txt)
    End If
End Sub
From the event you can call SetText(str).
 
Back
Top