Software Components Home

IMAP Email Body and Attachments Missing?

Back

Question:
(I am using the IMAP componet to connect to "microsoft exchange server")

1) When my code below runs the "email.Body" property is always blank..even though there is data in each email scanned?

2) The emails always contain attachments and the "Imap.GetMailNumAttach(email)" does return the correct number of attachments in each email

This statement does iterate though each attachment in each email ok. But the SaveAttachedFile statement does not actually save the filename (to the "c:\Temp" folder) No Attachments appear to be saved at all

                    For intii = 0 To Imap.GetMailNumAttach(email) - 1 
                        email.SaveAttachedFile(intii, "C:\temp") 
                    Next 
Please could you tell me what I am doing wrong

Answer:
The email.Body problem is due to the fact that you are only fetching the email headers. This line of code:
email = Bundle.GetEmail(idx)
does not retrieve the full email from the mail server. It simply fetches the email object from the bundle (no communications with the mail server are involved). Because you only retrieved headers, the email object is "half-baked" and certain things won't be there, like the body and attachments. However, the information about the attachments (filenames and sizes) should be present.

Dim Imap As New Chilkat.Imap 

        Try 

            'UnlockComponent begins a 30-day trial. 
            Imap.UnlockComponent("anything") 
            Imap.Connect(strMailServer) 
            Imap.Login(strMailServerUserName, strMailBoxServerPassword) 
            Imap.SelectMailbox(strMailBoxFolderName) 

            If Not Imap.IsConnected Then 
                strErrorMsg = "Cannot Connect to email server " & strMailServer & " 
                Username = " & strMailServerUserName & " Password = " &                                     strMailBoxServerPassword

                Return False 
                Exit Function 
            End If 

            Dim idx As Int32 = 0 
            Dim strMsgSubject As String = String.Empty 
            Dim strFileTypeToProcess As String = String.Empty 
            Dim intProject_Attactment_Num As Int32 = 0 
            Dim intProject_Log_Attactment_Num As Int32 = 0 

            Dim email As Chilkat.Email 
            Dim Bundle As New Chilkat.EmailBundle 
            Dim MsgSet As New Chilkat.MessageSet 

            MsgSet = Imap.Search("ALL", True) 'read all of the emails in strMailBoxFolderName 

            Bundle = Imap.FetchHeaders(MsgSet) 'and now get them 

            If (Bundle Is Nothing) Then 
                MsgBox(Imap.LastErrorText) 
                Exit Function 
            End If 

            intTotal_Emails = Bundle.MessageCount 

            Dim strBodyText As String 
            Dim strSubject As String 

            For idx = 0 To Bundle.MessageCount - 1 

                email = Bundle.GetEmail(idx) 

                strBodyText = email.Body.Trim 
                strSubject = email.Subject.Trim 

                 ' Save attachments to the "attachments" directory. 
                If Imap.GetMailNumAttach(email) > 0 Then 

                    Dim intii As Int32 = Imap.GetMailNumAttach(email) 

                    For intii = 0 To Imap.GetMailNumAttach(email) - 1 
                        email.SaveAttachedFile(intii, "C:\temp") 
                    Next 

                End If 

            Next idx 

            Imap.Logout() 
            Imap.Disconnect() 

            Return True 

        Catch ex As Exception 

            Dim strMsg As String = ex.Message 
            MessageBox.Show(strMsg) 

        Finally 

            If Not IsNothing(Imap) Then 
                Imap.Dispose() 
                Imap = Nothing 
            End If 

        End Try