如何在c#WPF中阅读gmail,yahoo,outlook .....中的所有邮件 [英] How to read all mails in gmail,yahoo,outlook..... in c# WPF

查看:122
本文介绍了如何在c#WPF中阅读gmail,yahoo,outlook .....中的所有邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阅读gmail,yahoo,outlook中的所有邮件...在c#WPF中



实际上我想扫描邮件服务器

解决方案

要阅读电子邮件,您需要使用IMAP协议。 .NET框架中默认不提供。所以,你必须自己实现它。从您的问题,我可以得出结论,你不知道如何在.NET中实现IMAP协议,所以采取我的建议,使用第三方库。



ImapX [ ^ ]是一个很棒的图书馆,我自己使用,测试和喜爱!你也应该看看,它会非常有用,因为你会得到一个库,所以你唯一要做的就是理解 IMAP协议 [ ^ ]。要访问邮箱,您应该考虑使用IMAP协议查询它们。它比POP3更好,因为邮件不会被拉到客户端的机器上,而且库很快,因为它允许你设置下载大小;仅限标记,仅限内容,附件等。


 以下查找示例 

使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Data;
使用 System.Windows.Documents;
使用 System.Windows.Input;
使用 System.Windows.Media;
使用 System.Windows.Media.Imaging;
使用 System.Windows.Navigation;
使用 System.Windows.Shapes;
使用 System.IO;
使用 System.Data;
使用 System.Configuration;
使用 System.Web;
使用 System.Net.NetworkInformation;
使用 System.Net.Security;
使用 System.Net.Sockets;
使用 ImapX;
使用 ImapX.Enums;

命名空间 MailServerScan
{
/// < 摘要 >
/// MainWindow.xaml $ b的交互逻辑$ b /// < < span class =code-summarycomment> / summary >
public partial class MainWindow:Window
{

public MainWindow()
{
InitializeComponent();
}


私有 void button1_Click(< span class =code-keyword> object sender,RoutedEventArgs e)
{
Read_Emails();
}



private void Read_Emails()
{

try
{
ImapX.ImapClient client = new ImapX.ImapClient( imap.gmail.com 993 true );
if (client.Connect())
{
if (client.Login( xxxxx@gmail.com 密码))
{

foreach var foldername in client.Folders)
{

foreach var item 客户端中。文件夹[foldername.Name] .Search( ALL))
{
string val = item.Body.Text;

if (!string.IsNullOrEmpty(val.Trim()))
{
MessageBox.Show(foldername .Name + + val);
}

var folderPath = @ C:\attachments;

foreach var file in item.Attachments)
{
file.Download(); // 正在下载附件
file.Save(folderPath / * ,可选文件名* / );
}
}
}

client.Logout();
}
else
{
MessageBox.Show( 登录失败);
}
}
else
{
MessageBox.Show( Connection_Failed);
}

}
catch (Exception ex)
{
MessageBox.Show( ex.Message);
}

}
}
}





注意



如果您使用Gmail,则应启用访问安全性较低的应用:启用


How to read all mails in gmail,yahoo,outlook..... in c# WPF

Actually i want to scan mailserver

解决方案

For reading the emails, you need to use IMAP protocol. Which is not provided by default in .NET framework. So, you have to implement it yourself. From your question, I can conclude that you have no idea of "how to implement the IMAP protocol in .NET", so take my recommendation, use a third-party library.

ImapX[^] is a great library that I have myself used, tested and loved! You should also give it a look, it would be very useful as you would get a library so the only thing you would have to do is to understand the IMAP protocol[^]. To access the mailboxes, you should consider querying them using IMAP protocol. It is better than POP3, because the mails are not pulled to the client's machine, and the library is fast as it allows you to set a download size; flags only, content only, with attachments etc.


Below find example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using ImapX;
using ImapX.Enums;

namespace MailServerScan
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Read_Emails();
        }



        private void Read_Emails()
        {

            try
            {
                ImapX.ImapClient client = new ImapX.ImapClient("imap.gmail.com", 993, true);
                if (client.Connect())
                {
                    if (client.Login("xxxxx@gmail.com", "password"))
                    {

                        foreach (var foldername in client.Folders)
                        {

                            foreach (var item in client.Folders[foldername.Name].Search("ALL"))
                            {
                                string val = item.Body.Text;

                                if (!string.IsNullOrEmpty(val.Trim()))
                                {
                                    MessageBox.Show(foldername.Name + " : " + val);
                                }

                                var folderPath = @"C:\attachments";

                                foreach (var file in item.Attachments)
                                {
                                    file.Download(); // Downloading the attachment
                                    file.Save(folderPath /*, optional file name*/);
                                }
                            }
                        }

                        client.Logout();
                    }
                    else
                    {
                        MessageBox.Show("Login Failed");
                    }
                }
                else
                {
                    MessageBox.Show("Connection_Failed");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}



Note:

If you use Gmail you should to enable "Access for less secure apps :Turn on"


这篇关于如何在c#WPF中阅读gmail,yahoo,outlook .....中的所有邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆