如何使用C#.net从Gmail读取邮件 [英] How to read mail from Gmail using C#.net

查看:81
本文介绍了如何使用C#.net从Gmail读取邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在开发Web应用程序,我必须配置gmail。我想在网格视图中显示所有收件箱邮件,其中主题按降序排列(日期明确)。我尝试了很多代码,但没有找到好的工作。



在ASP.NET中读取Gmail收件箱消息 [ ^ ]





[ ^ ]



我使用了这个,但它只显示了几封电子邮件和升序(日期)。

请为我提供良好的链接,对此工作正常。



还告诉我使用IMAP或POP3哪个好。

我有一些其他代码,但适用于雅虎而不适用于gmail。



在此先感谢

Neetesh Agarwal

Hello all,

I am working on Web Application in which I have to configure gmail . I want to show all Inbox mail in Grid view with Subject in descending Order(date wise) . I tried many code but not found good which work.

Read Gmail Inbox Message in ASP.NET[^]


[^]

I used this one but Its only show only few emails and in Ascending Order(Datewise).
Please provide me good link which work fine for this.

Also Tell me which is good to use IMAP or POP3.
I have some other code but that works for yahoo not for gmail.

Thanks In Advance
Neetesh Agarwal

推荐答案

在ASP.NET中读取Gmail收件箱消息 [ ^ ]


使用IMAP insted Pop3 Server:

Using IMAP insted of Pop3 Server:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;

namespace mail
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Imap client = new Imap();
            // connect to server

            client.Connect("imap.gmail.com", 993, SslMode.Implicit);

            // authenticate
            client.Login("username", "password");

            // select folder
            client.SelectFolder("Inbox");

            int NoOfEmailsPerPage = 10;
            int totalEmails = client.CurrentFolder.TotalMessageCount;
            // get message list - envelope headers
            ImapMessageCollection messages = client.GetMessageList(ImapListFields.Envelope);

            // display info about each message
          
           
            foreach (ImapMessageInfo message in messages)
            {
                
                TableCell noCell = new TableCell();
                
                noCell.CssClass = "emails-table-cell";

                noCell.Text = Convert.ToString(message.To);
                TableCell fromCell = new TableCell();
                fromCell.CssClass = "emails-table-cell";
                fromCell.Text = Convert.ToString(message.From);
                TableCell subjectCell = new TableCell();
                subjectCell.CssClass = "emails-table-cell";
                subjectCell.Style["width"] = "300px";
                subjectCell.Text = Convert.ToString(message.Subject);
                TableCell dateCell = new TableCell();
                dateCell.CssClass = "emails-table-cell";
                if (message.Date.OriginalTime != DateTime.MinValue)
                    dateCell.Text = message.Date.OriginalTime.ToString();
                TableRow emailRow = new TableRow();
                emailRow.Cells.Add(noCell);
                emailRow.Cells.Add(fromCell);
                emailRow.Cells.Add(subjectCell);
                emailRow.Cells.Add(dateCell);
                EmailsTable.Rows.AddAt(2 + 0, emailRow);
                
            }
            int totalPages;
            int mod = totalEmails % NoOfEmailsPerPage;
            if (mod == 0)
                totalPages = totalEmails / NoOfEmailsPerPage;
            else
                totalPages = ((totalEmails - mod) / NoOfEmailsPerPage) + 1;

          

        }
    }
}


查看此链接 [ ^ ]


这篇关于如何使用C#.net从Gmail读取邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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