Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4253758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:59:21+00:00 2026-05-21T04:59:21+00:00

I want to convert the following vb.net script into C#.net script in SSIS Script

  • 0

I want to convert the following vb.net script into C#.net script in SSIS Script task.
I tried converting using Developer Fusion but iam getting lots of errors.Can some one help me.My Vb.net script:

    Public Sub GetAttachments()
        Dim myolApp As Outlook.Application
        Dim ns As Outlook.NameSpace
        Dim Inbox As MAPIFolder
        Dim Item As Object
        Dim Atmt As Attachment
        Dim FileName As String
        Dim subject As String
        Dim AttachmentName As String
        Dim Body As String
        Dim SenderName As String
        Dim SenderEmailAddress As String
        Dim CreationTime As String

        Dim i As Integer
        Dim j As Integer
        On Error GoTo GetAttachments_err
        myolApp = CreateObject("Outlook.Application")
        ns = myolApp.GetNamespace("MAPI")
        ns.Logon("", "", False, True)
        Inbox = ns.Folders("Mailbox - name").Folders("Inbox")
        i = 0
        j = 1

       'Scan for attachments
        For Each Item In Inbox.Items() 
            System.Windows.Forms.Application.DoEvents()
            If (Item.UnRead) Then

                MessageBox.Show(j, "EMail Number")
                subject = DirectCast(Item, Microsoft.Office.Interop.Outlook.MailItem).Subject
                MessageBox.Show(subject, "E-Mail Subject")
                AttachmentName = DirectCast(Item, Microsoft.Office.Interop.Outlook.MailItem).Attachments(1).FileName
                Body = DirectCast(Item, Microsoft.Office.Interop.Outlook.MailItem).Body
                SenderEmailAddress = DirectCast(Item, Microsoft.Office.Interop.Outlook.MailItem).SenderEmailAddress
                SenderName = DirectCast(Item, Microsoft.Office.Interop.Outlook.MailItem).SenderName
                CreationTime = DirectCast(Item, Microsoft.Office.Interop.Outlook.MailItem).CreationTime.ToString()
                MessageBox.Show(AttachmentName, "Attachment Name")
                MessageBox.Show(Body, "Body")
                MessageBox.Show(SenderEmailAddress, "From Address")
                MessageBox.Show(SenderName, "From")
                MessageBox.Show(CreationTime, "Created Time")
                j = j + 1
                If (SenderEmailAddress.ToLower = "lch@gmail.com".ToLower) Then
                    MessageBox.Show("reading from")
                    For Each Atmt In Item.Attachments
                        FileName = "C:\Email Attachments\" & Atmt.FileName
                        Atmt.SaveAsFile(FileName)
                        i = i + 1
                        Item.UnRead = True
                    Next Atmt
                End If
            End If
        Next Item

        'Display summary
        If i > 0 Then
            MsgBox("I found " & i & " attached files." _
               & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
               & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!")
        Else
            MsgBox("I didn't find any attached files in your mail.", vbInformation, _
            "Finished!")
        End If

        'Clear Memory
GetAttachments_exit:
        Atmt = Nothing
        Item = Nothing
        ns = Nothing
        Exit Sub

        'Error Handler
GetAttachments_err:
        MsgBox("An unexpected error has occurred." _
           & vbCrLf & "Please note and report the following information." _
           & vbCrLf & "Script Name: GetAttachments" _
           & vbCrLf & "Error Number: " & Err.Number _
           & vbCrLf & "Error Description: " & Err.Description _
           & vbCrLf & "Error Line: " & Err.Source _
           , vbCritical, "Error!")
        Resume GetAttachments_exit
        'End If

    End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:59:21+00:00Added an answer on May 21, 2026 at 4:59 am

    Add reference to Microsoft.VisualBasic

    add using statements:

    using Microsoft.Office.Interop.Outlook;
    using Microsoft.VisualBasic;
    
     public void GetAttachments()
    {
    Microsoft.Office.Interop.Outlook.Application myolApp = default(Microsoft.Office.Interop.Outlook.Application);
    Microsoft.Office.Interop.Outlook.NameSpace ns = default(NameSpace);
    MAPIFolder Inbox = default(MAPIFolder);
    object Item = null;
    Attachment Atmt = default(Attachment);
    string FileName = null;
    string subject = null;
    string AttachmentName = null;
    string Body = null;
    string SenderName = null;
    string SenderEmailAddress = null;
    string CreationTime = null;
    
    int i = 0;
    int j = 0;
    try
    {
    
        myolApp = (Microsoft.Office.Interop.Outlook.Application)Interaction.CreateObject("Outlook.Application","");
        ns = myolApp.GetNamespace("MAPI");
        ns.Logon("", "", false, true);
        Inbox = ns.Folders["Mailbox - name"].Folders["Inbox"];
        i = 0;
        j = 1;
    
        //Scan for attachments
        foreach (object Item_loopVariable in Inbox.Items) {
            Item = Item_loopVariable;
            System.Windows.Forms.Application.DoEvents();
    
            if ((Item as MailItem) != null ? ((MailItem)Item).UnRead : false) {
                MessageBox.Show(j.ToString(), "EMail Number");
                subject = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Subject;
                MessageBox.Show(subject, "E-Mail Subject");
                AttachmentName = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Attachments[1].FileName;
                Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
                SenderEmailAddress = ((Microsoft.Office.Interop.Outlook.MailItem)Item).SenderEmailAddress;
                SenderName = ((Microsoft.Office.Interop.Outlook.MailItem)Item).SenderName;
                CreationTime = ((Microsoft.Office.Interop.Outlook.MailItem)Item).CreationTime.ToString();
                MessageBox.Show(AttachmentName, "Attachment Name");
                MessageBox.Show(Body, "Body");
                MessageBox.Show(SenderEmailAddress, "From Address");
                MessageBox.Show(SenderName, "From");
                MessageBox.Show(CreationTime, "Created Time");
                j = j + 1;
                if ((SenderEmailAddress.ToLower() == "lch@gmail.com".ToLower())) {
                    MessageBox.Show("reading from");
                    foreach (Attachment att in ((MailItem)Item).Attachments ) {
                        FileName = "C:\\Email Attachments\\" + att.FileName;
                        att.SaveAsFile(FileName);
                        i = i + 1;
                        ((MailItem)Item).UnRead = true;
                    }
                }
            }
        }
    
        //Display summary
        if (i > 0) {
            MessageBox.Show("I found " + i + " attached files." + "\r\n" + "I have saved them into the C:\\Email Attachments folder." + "\r\n" + "\r\n" + "Have a nice day.", "Finished!");
        } else {
            MessageBox.Show("I didn't find any attached files in your mail.", "Finished!");
        }
    
            //Clear Memory
            Atmt = null;
            Item = null;
            ns = null;
    
    }
    catch (System.Exception ex)
    {
            MessageBox.Show("An unexpected error has occurred." 
           + "\r\n" + "Please note and report the following information."
           + "\r\n" + "Script Name: GetAttachments"
           + "\r\n" + "Error Description: " + ex.Message
           + "\r\n" + "Error StackTrace: " + ex.StackTrace
           , "Error!");
            Atmt = null;
            Item = null;
            ns = null;
    }
    }
    

    Give this a try and let me know, I’ve changed the exception details a bit so let me know if you want to change something else. Also, I’ve removed the goto statement and replaced it with a try catch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
I want to convert a primitive to a string, and I tried: myInt.toString(); This
I want to convert a string into a double and after doing some math
I want to convert mouse's current X and Y coordinates into the 3D space
Possible Duplicate: Convert HTML to PDF in .NET I am using VSTS 2008 +
The following is working, but my Body.NodeType changes to Convert , instead of MemberAccess
I want to convert an instance of generic IDictionary<,> to non generic IDictionary .
I want to convert a number that is in PRTime format (a 64-bit integer
I want to convert a number between 0 and 4096 ( 12-bits ) to
I want to convert an XML document containing many elements within a node (around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.