need to copy OTP

Asma Zia 0 Reputation points
2023-02-03T05:20:01.1833333+00:00

Hello,

My use case is that my user has an outlook account and I am doing automation so when a person login a 2FA is required,

so I need to copy the OTP and paste it into my application

I m writing code in c#, is there any way to connect to outlook?

my code is below but nothing showing in console

using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace new project
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
            NameSpace outlookNS = outlookApp.GetNamespace("MAPI");
            MAPIFolder inbox = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Items items = inbox.Items;

            foreach (object item in items)
            {
                if (item is MailItem mailItem)
                {
                    if (mailItem.Subject.Contains("One-Time Password for Your Account"))
                    {
                        string body = mailItem.Body;
                        int startIndex = body.IndexOf("OTP:") + 4;
                        int length = 6;
                        string otp = body.Substring(startIndex, length);
                        Console.WriteLine("OTP: " + otp);
                        break;
                    }
                }
            }
        }
    }
}


C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes