【解決方法】C#.net を使用して Gmail からメールを読む方法

プログラミングQA


こんにちは皆さん、

私は gmail を設定する必要がある Web アプリケーションに取り組んでいます。 すべての受信トレイメールをグリッドビューで件名を降順(日付順)に表示したいと考えています。 多くのコードを試しましたが、うまく機能するものは見つかりませんでした。

ASP.NETでGmailの受信トレイメッセージを読む[^]

[^]

私はこれを使用しましたが、少数のメールしか表示されず、昇順(日付順)で表示されます。
これにうまく機能する適切なリンクを提供してください。

また、IMAPとPOP3どちらを使用するのが良いか教えてください。
他にもコードがありますが、それはgmailではなくyahooでは機能します。

前もって感謝します
ニーテシュ・アガルワル

解決策 1

解決策 2

Pop3 サーバーの代わりに IMAP を使用する:

C#
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;

          

        }
    }
}

解決策 3

コメント

タイトルとURLをコピーしました