Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在电子邮件应用程序中请求凭据_C#_Visual Studio_Email_Outlook - Fatal编程技术网

C# 在电子邮件应用程序中请求凭据

C# 在电子邮件应用程序中请求凭据,c#,visual-studio,email,outlook,C#,Visual Studio,Email,Outlook,我需要在visual studio中制作一个应用程序,以检索收件箱、从exchange电子邮件帐户发送的邮件和附件,并能够标记为已读并删除所述邮件(如果愿意,可以使用类似outlook的应用程序) 我有从outlook检索收件箱和附件的代码,但我需要它与outlook分开,或者至少我需要它能够在显示邮件之前请求凭据。这是我的代码: Outlook.Application Application = new Outlook.Application(); Outlook.NameSpace Outl

我需要在visual studio中制作一个应用程序,以检索收件箱、从exchange电子邮件帐户发送的邮件和附件,并能够标记为已读并删除所述邮件(如果愿意,可以使用类似outlook的应用程序)

我有从outlook检索收件箱和附件的代码,但我需要它与outlook分开,或者至少我需要它能够在显示邮件之前请求凭据。这是我的代码:

Outlook.Application Application = new Outlook.Application();
Outlook.NameSpace OutlookNameSpace =  Application.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;

//Fills a listbox with the inbox messages
public void fillInboxListBox()
{    
    for (int i = 1; i <= inbox.Items.Count; i++)
    {
       //lsbxMessages is a ListBox control 
       lsbxMessages.Items.Add(((Outlook.MailItem)inbox.Items[i]).Subject);
    }                
}

//When an item on the listbox is selected, it loads the mail on a separate rich text box
private void lsbxMessages_SelectedIndexChanged(object sender, EventArgs e)
{
     //lblSubject is a label where I put the subject of the message
     lblSubject.Text = ((Outlook.MailItem)inbox.Items[(lsbxMessages.SelectedIndex + 1)]).Subject;

     //lblSender this is where I put the sender name and email adress         
     lblSender.Text = ((Outlook.MailItem)inbox.Items[(lsbxMessages.SelectedIndex + 1)]).SenderName + " <" + ((Outlook.MailItem)inbox.Items[(lsbxMessages.SelectedIndex + 1)]).SenderEmailAddress + ">";

     //I put the body of the email in a richtextbox         
     rtbBody.Text = ((Outlook.MailItem)inbox.Items[(lsbxMessages.SelectedIndex + 1)]).Body;

     //lblDateSent shows the date the message was sent
     lblDateSent.Text = ((Outlook.MailItem)inbox.Items[(lsbxMessages.SelectedIndex + 1)]).SentOn.ToString();

     //rtbReceiver is a rich text box where I show all the adresses the email was sent to
     rtbReceiver.Text = ((Outlook.MailItem)inbox.Items[(lsbxMessages.SelectedIndex + 1)]).To;
}
Outlook.Application=新建Outlook.Application();
Outlook.NameSpace OutlookNameSpace=Application.GetNamespace(“MAPI”);
Outlook.MAPIFolder inbox=OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
项目=收件箱。项目;
//用收件箱消息填充列表框
public void fillInboxListBox()
{    

对于(int i=1;i您可以通过任何要设计的对话框进行询问,然后必须通过
名称空间.LogOn
方法将参数传递给Outlook。默认情况下,您打开的是当前用户的默认配置文件

这是一个很好的教程(虽然它只显示登录到默认配置文件而不要求用户名,但添加您自己的对话框以获取用户名/密码并不重要):

还需要注意的是,您不能创建Outlook的另一个实例。如果Outlook已在运行,则必须获取正在运行的应用程序的实例。(您可以创建另一个“实例”,但它不会创建新的进程,它将使用现有进程和加载的配置文件).即使这样,我也不认为您一次可以加载多个配置文件