locked
How to download attachment files using pop3 in VB.net??? RRS feed

  • Question

  • User-494525173 posted

     Hi,

    Can anyone please help with the code, how to download attachments files using pop3(received mails) in VB.net

    Thanks in advance!!!!!!

    Friday, May 15, 2009 1:51 AM

Answers

  • User-1659704165 posted

     Dim inBox As Outlook.MAPIFolder = Me.ActiveExplorer() _
            .Session.GetDefaultFolder(Outlook. _
            OlDefaultFolders.olFolderInbox)
        Dim inBoxItems As Outlook.Items = inBox.Items
        Dim newEmail As Outlook.MailItem
        inBoxItems = inBoxItems.Restrict("[Unread] = true")
        Try
            For Each collectionItem As Object In inBoxItems
                newEmail = TryCast(collectionItem, Outlook.MailItem)
                If newEmail IsNot Nothing Then
                    If newEmail.Attachments.Count > 0 Then
                        For i As Integer = 1 To newEmail.Attachments.Count
                            Dim saveAttachment As Outlook.Attachment = _
                                newEmail.Attachments(i)
                            newEmail.Attachments(i).SaveAsFile _
                                ("C:\TestFileSave\" & (newEmail _
                                .Attachments(i).FileName))
                        Next i
                    End If
                End If
            Next collectionItem
        Catch ex As Exception
            If Left(ex.Message, 11) = "Cannot save" Then
                MessageBox.Show("Create Folder C:\TestFileSave")
            End If
        End Try

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2009 2:42 AM

All replies

  • User-1659704165 posted

     Hi,


    Add Refernce -> Com Tab-> microsoft Outlook libary



    /**************Code******************/



    private void ThisApplication_NewMail()
    {
     Outlook.Applocation  outlookApp =  new  Outlook.Applocation();
        Outlook.MAPIFolder inBox = outlookApp.ActiveExplorer()
            .Session.GetDefaultFolder(Outlook
            .OlDefaultFolders.olFolderInbox);
        Outlook.Items inBoxItems = inBox.Items;
        Outlook.MailItem newEmail = null;
        inBoxItems = inBoxItems.Restrict("[Unread] = true");
        try
        {
            foreach (object collectionItem in inBoxItems)
            {
                newEmail = collectionItem as Outlook.MailItem;
                if (newEmail != null)
                {
                    if (newEmail.Attachments.Count > 0)
                    {
                        for (int i = 1; i <= newEmail
                           .Attachments.Count; i++)
                        {
                            newEmail.Attachments[i].SaveAsFile
                                (@"C:\TestFileSave\" +
                                newEmail.Attachments[i].FileName);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string errorInfo = (string)ex.Message
                .Substring(0, 11);
            if (errorInfo == "Cannot save")
            {
                MessageBox.Show(@"Create Folder C:\TestFileSave");
            }
        }
    }



    /************************************/


    Friday, May 15, 2009 2:42 AM
  • User-1659704165 posted

     Dim inBox As Outlook.MAPIFolder = Me.ActiveExplorer() _
            .Session.GetDefaultFolder(Outlook. _
            OlDefaultFolders.olFolderInbox)
        Dim inBoxItems As Outlook.Items = inBox.Items
        Dim newEmail As Outlook.MailItem
        inBoxItems = inBoxItems.Restrict("[Unread] = true")
        Try
            For Each collectionItem As Object In inBoxItems
                newEmail = TryCast(collectionItem, Outlook.MailItem)
                If newEmail IsNot Nothing Then
                    If newEmail.Attachments.Count > 0 Then
                        For i As Integer = 1 To newEmail.Attachments.Count
                            Dim saveAttachment As Outlook.Attachment = _
                                newEmail.Attachments(i)
                            newEmail.Attachments(i).SaveAsFile _
                                ("C:\TestFileSave\" & (newEmail _
                                .Attachments(i).FileName))
                        Next i
                    End If
                End If
            Next collectionItem
        Catch ex As Exception
            If Left(ex.Message, 11) = "Cannot save" Then
                MessageBox.Show("Create Folder C:\TestFileSave")
            End If
        End Try

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2009 2:42 AM
  • User-494525173 posted

    Thank you for quick reply.

     

    I dont want to download files from outlook.

    I want to download attachments from some aaaa@gmail.com using pop3 protocol through vb.net

     

     

    Friday, May 15, 2009 3:49 AM
  • User-1936035690 posted
    Hi,
    Short of writing your own POP3 client, there isn't any built-in functionality in .NET to do this.

    You can use a commercial component, like mine - aspNetPOP3.com

    or if you want something more open source, I believe there are some code examples on codeproject to get you started.

    Cheers!
    Dave
    Friday, May 15, 2009 2:56 PM