首先要有個 gmail 的帳號(廢話...)
然後記得要去設定信件可以用 pop3 收信的項目
最後就剩下寫程式了!

另外連 gmail pop3 server 必須要使用 SSL
所以我額外用了一個 libarary 來連線
OpenPOP.NET
有了它之後就方便多啦~
以下為範例:

        private readonly POPClient popClient = new POPClient();

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // gmail
            if (popClient.Connected)
                popClient.Disconnect();
            popClient.Connect("pop.gmail.com", 995, true);
            popClient.Authenticate(this.txtUser.Text, this.txtPassword.Text);
            // 信件數量
            int Count = popClient.GetMessageCount();

            int success = 0;
            int fail = 0;
            for (int i = Count; i >= 1; i -= 1)
            {
                // 取得信件
                OpenPOP.MIME.Message m = popClient.GetMessage(i);
                if (m != null)
                {
                    success++;
                    MessageBox.Show("[" + i + "] " + m.Headers.Subject);
                }
                else
                {
                    fail++;
                }
            }
            MessageBox.Show(this, "Mail received!\nSuccess: " + success + "\nFailed: " + fail);
        }
arrow
arrow
    全站熱搜

    JohnDX 發表在 痞客邦 留言(0) 人氣()