1
你們看到這個C#代碼的任何問題嗎?它從Gmail電子郵件通知,然後打印到CMD多少未讀郵件正在等待:我的Gmail通知程序代碼有什麼問題?
Unread Mails: 10
Unread Mails: 10
,然後發送了多少郵件通過串行過。但在它說「未讀郵件:」兩次我得到:
Operation Timed Out.
Unread Mails: 0
它重複。
Iv'e在不同的計算機和互聯網服務提供商(ISP)上試用過它,所以它在代碼中是絕對的東西。 C#程序。我也曾嘗試更改Thread.Sleep值,以便在再次執行此操作之前花費更長時間,但仍然無效。 謝謝!
public static void Main(string[] args)
{
try
{
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
string Unreadz = "0";
while (true)
{
Unreadz = CheckMail();
Console.WriteLine("Unread Mails: " + Unreadz);
if (Convert.ToInt32(Unreadz) < 10) port.Write("0" + Unreadz);
else port.Write("" + Unreadz);
System.Threading.Thread.Sleep(10000);
}
} catch (Exception ee) { Console.WriteLine(ee.Message); }
}
public static string TextToBase64(string sAscii)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytes = encoding.GetBytes(sAscii);
return System.Convert.ToBase64String(bytes, 0, bytes.Length);
}
public static string CheckMail()
{
string result = "0";
try
{
var url = @"https://gmail.google.com/gmail/feed/atom";
var USER = "USER";
var PASS = "PASS";
var encoded = TextToBase64(USER + ":" + PASS);
var myWebRequest = HttpWebRequest.Create(url);
myWebRequest.Method = "POST";
myWebRequest.ContentLength = 0;
myWebRequest.Headers.Add("Authorization", "Basic " + encoded);
var response = myWebRequest.GetResponse();
var stream = response.GetResponseStream();
XmlReader reader = XmlReader.Create(stream);
while (reader.Read())
if (reader.NodeType == XmlNodeType.Element)
if (reader.Name == "fullcount")
{
result = reader.ReadElementContentAsString();
return result;
}
} catch (Exception ee) { Console.WriteLine(ee.Message); }
return result;
}
}
}