Question Exception Error, Help please

atr232000

Member
Joined
Jan 9, 2010
Messages
17
Programming Experience
Beginner
Hi All

I have created a little mail back up to sql and read back application.

However when it goes through my mail items in outlook i get an arror and i can not figure why. I thought it might have been a particular email but if happens randomly. Below is my read mail code

VB.NET:
For Each oMail In oParentFolder.Items
                If oMail.ReceivedTime.ToOADate < DateAdd(DateInterval.Day, NumericUpDown1.Value * -1, Now).ToOADate Then

                    Dim EmailBody As String = oMail.HTMLBody
                    oMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
                    EmailBody = oMail.HTMLBody.ToString.Replace("'", "''")
                    Dim myEncodedString As String
                    myEncodedString = HttpUtility.HtmlEncode(EmailBody)
                    Debug.WriteLine("HTML Encoded string is " + myEncodedString)

                    Dim cced As String = checkstring(oMail.CC)
                    Dim Bcced As String = checkstring(oMail.BCC)
                    Dim Subj As String = checkstring(oMail.Subject)

                    Dim fixedSub As String = Replace(Subj)

                    If oMail.Attachments.Count <> 0 Then
                        attachment = True
                    End If
                    Dim CurrentID As String = FetchandUpdateID(sqlconn)

                    sqlInsert.CommandText = "Insert into EmItem ([ID],[EmFrom],[EmTo],[EmCCed],[EmBCCed],[EmSub],[EmPri],[EmBody],[EmTime],[EmAtt],[UserName])" & _
                    "Values ('" & CurrentID & "','" & oMail.SenderEmailAddress.Replace("'", "") & "','" & oMail.To.Replace("'", "") & "','" & cced.Replace("'", "") & "','" & Bcced.Replace("'", "") & "','" & fixedSub & "','" & oMail.Importance & "','" & myEncodedString & "','" & oMail.ReceivedTime.ToString(Format("dd MM yyyy HH:mm:ss")) & "','" & attachment & "','" & SYSTEMUsername & "')"
                    whereError = "Insert Email Error"
                    sqlInsert.ExecuteNonQuery()

                    Dim EmailID As Integer = 0

                    If attachment = True Then
                        Dim AttCount As Integer = 0
                        For Each item As Outlook.Attachment In oMail.Attachments

                            Dim file As String = "c:\Attachments\" & item.FileName.ToString
                            item.SaveAsFile(file)


                            Dim fs As FileStream = New FileStream(file, FileMode.Open)
                            Dim img As Byte() = New Byte(fs.Length) {}
                            imagebyte = img
                            fs.Read(img, 0, fs.Length)
                            fs.Close()

                            Dim imgLength As Integer = item.FileName.Length
                            Dim imgType As String = Path.GetExtension(item.FileName)
                            ext = Path.GetExtension(item.FileName)
                            mImageFile = Nothing

                            Dim strConnect As String
                            strConnect = My.Settings.SQLConn

                            Dim conn As SqlConnection = New SqlConnection(strConnect)

                            Dim sSQL As String = "INSERT INTO EmAtt ([EmID],[EmAttName],ImageContent, " & _
                                    "ImageTitle, ImageType) VALUES('" & CurrentID & "','" & item.FileName.ToString & "', @pic, @title, @itype)"

                            Dim cmd As SqlCommand = New SqlCommand(sSQL, conn)

                            ' image content
                            Dim pic As SqlParameter = New SqlParameter("@pic", SqlDbType.Image)
                            pic.Value = img
                            cmd.Parameters.Add(pic)

                            ' title
                            Dim title As SqlParameter = New SqlParameter("@title", System.Data.SqlDbType.VarChar, 50)
                            title.Value = item.FileName.ToString()
                            cmd.Parameters.Add(title)

                            ' type
                            Dim itype As SqlParameter = New SqlParameter("@itype", System.Data.SqlDbType.Char, 4)
                            itype.Value = ext.ToString()
                            cmd.Parameters.Add(itype)

                            conn.Open()
                            cmd.ExecuteNonQuery()
                            conn.Close()
                            Kill(file)

                        Next

                    End If

                End If
            Next oMail

            oMail = Nothing

and here is the ex.message

VB.NET:
"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

Thanks in advance!!
 
A 'mail' folder in Outlook may contain both mail and post items, represented by MailItem and PostItem types. I guess that is what you occationally encounter.
 
Ok I'm a spanner

Thanks for your reply.

Im going to look like a spanner now!!

Whats the difference between a mailitem as postitem then??

And how do I tell if something In my inbox is mail or post!!

Thanks :)
 
Back
Top