简体   繁体   English

如何使用C#从gmail读取电子邮件

[英]how to read email from gmail using c#

I want to create window application through which i can read email from gmail. 我想创建一个窗口应用程序,通过它我可以读取来自gmail的电子邮件。

Actually i want to read proper format of email like to,from,subject,cc and body. 实际上,我想阅读正确的电子邮件格式,例如,主题,抄送和正文。

        using (Imap imap = new Imap())
        {
            imap.ConnectSSL("mail.company.com");
            imap.Login("angel_y@company.com", "xyx***");

            imap.SelectInbox();
            List<long> uids = imap.SearchFlag(Flag.Unseen);
            foreach (long uid in uids)
            {
                string eml = imap.GetMessageByUID(uid);
                IMail message = new MailBuilder()
                    .CreateFromEml(eml);

                Console.WriteLine(message.Subject);
                Console.WriteLine(message.TextDataString);
            }
            imap.Close(true);
        }    

It is this error. 就是这个错误。 No connection could be made because the target machine actively refused it 由于目标机器主动拒绝连接,因此无法建立连接

Try this I have added the Port number along with the gmail imap server for connection to the server 试试这个,我已经添加了端口号以及gmail imap服务器,用于连接到服务器

    using (Imap imap = new Imap())
    {
        imap.ConnectSSL("imap.gmail.com", 993);
        imap.Login("angel_y@company.com", "xyx***"); // MailID As Username and Password

        imap.SelectInbox();
        List<long> uids = imap.SearchFlag(Flag.Unseen);
        foreach (long uid in uids)
        {
            string eml = imap.GetMessageByUID(uid);
            IMail message = new MailBuilder()
                .CreateFromEml(eml);

            Console.WriteLine(message.Subject);
            Console.WriteLine(message.TextDataString);
        }
        imap.Close(true);
    } 

I am sure there are many libraries to do this. 我确信有很多图书馆可以做到这一点。 A quick search turned this up: 快速搜索发现:

http://code.msdn.microsoft.com/CSharpGmail http://code.msdn.microsoft.com/CSharpGmail

And here is a gadget / widget app that has some code to do this: http://www.codeproject.com/KB/gadgets/GadgetInterop.aspx 这是一个小工具/小工具应用程序,具有一些执行此操作的代码: http : //www.codeproject.com/KB/gadgets/GadgetInterop.aspx

You may need to make sure you are using the correct hostname and port number. 您可能需要确保使用正确的主机名和端口号。 Configuring these settings will depend on the IMAP API you are using for .Net 配置这些设置将取决于您用于.Net的IMAP API

But the settings you want to use are listed on google's site. 但是您要使用的设置已在Google网站上列出。

  • IMAP => imap.google.com:993 (SSL) IMAP => imap.google.com:993(SSL)
  • SMTP => smtp.google.com:587 (TLS) SMTP => smtp.google.com:587(TLS)

gmail offers access via its config page to pull down emails through POP3/IMAP. gmail可通过其配置页面提供访问权限,以通过POP3 / IMAP提取电子邮件。 Here are a few such links I found off of Google that could be used for IMAP access. 这是我在Google上发现的一些此类链接,可用于IMAP访问。

http://www.codeproject.com/KB/IP/imaplibrary.aspx http://www.codeproject.com/KB/IP/imaplibrary.aspx

Accessing Imap in C# 在C#中访问Imap

http://koolwired.com/solutions/solutions.aspx?id=30 http://koolwired.com/solutions/solutions.aspx?id=30

Hopefully that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM