我的 Gmail 通知程序代码有什么问题?
你们发现这段 C# 代码有什么问题吗?它从 gmail 获取电子邮件通知,然后向 CMD 打印有多少未读邮件正在等待:
Unread Mails: 10
Unread Mails: 10
然后也通过串行发送多少邮件。但在它说“未读邮件:”两次之后,我得到:
Operation Timed Out.
Unread Mails: 0
并且它重复。
我在不同的计算机和 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;
}
}
}
Do you guys see any issues with this C# code? It gets email notifications from gmail and then prints to CMD how many unread mails are waiting:
Unread Mails: 10
Unread Mails: 10
and then sends how many mails over serial too. But after it says "unread mails:" twice i get:
Operation Timed Out.
Unread Mails: 0
and it repeats.
Iv'e tried this on different computers and ISPs so its definitely something in the code.
The C# program. I also have tried changing the Thread.Sleep value so that it takes longer before it does it again, but still doesn't work.
Thanks!
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我似乎解决了它。因为我只是每 10 秒循环一次进行测试,所以我将其设置为 5 分钟。 5分钟更符合我的需求。
well i seemed to fix it. since i was just looping it every 10 seconds for testing, i put it to 5 minutes. 5 minutes is more realistic for my needs.