开发者

C# gmail notifer code. make it repeat?

this code checks if i have mail from gmail, then sends m over serial to a arduino if their is mail.

Ok so my code wont compile (error saying object reference required on code"CheckMail();") unless i change Main to non static like it is now , then it wont compile because theirs no static Main, im stumped.

also, how do i make this repeat so that it checks for messages every minute?

Thanks!!!

EDIT\\ OK YES!!! i got a working com emualtor finally. after your guys help it works but it only refershes twice (instantly not 60sec apart). the CMD stays open, it just stops after saying Unread Mail:2 Unread Mail:2 then i get operation timed out, Unread Mail:0 even though i have unread mails. even if i change { System.Threading.Thread.Sleep(10000 * 60); }开发者_开发知识库 to a longer time it still has same time between refreshing the first two times.

(the namespace, class and using Sytem etc arent in here.)

    public void Main(string[] args)
    {

        try {
            SerialPort port = new SerialPort( "COM9", 9600, Parity.None, 8, StopBits.One );
            port.Open();

            string Unreadz = "0";
            while ( true ) {
                Unreadz = CheckMail();
                Console.WriteLine( "Unread Mails: " + Unreadz );

                if ( !Unreadz.Equals( "0" ) ) port.Write( "m" );
                else port.Write( "n" );

                System.Threading.Thread.Sleep(1000);
            }
        } 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);
    }

    private string CheckMail() {
        string result = "0";

        try {
            var url = @"https://gmail.google.com/gmail/feed/atom";
            var USER = "username";
            var PASS = "password";

            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;
    }


Make CheckMail static as well.


while ( true ) {
  try {
    SerialPort port = new SerialPort( "COM9", 9600, Parity.None, 8, StopBits.One );
    port.Open();

    string Unreadz = "0";

      Unreadz = CheckMail();
      Console.WriteLine( "Unread Mails: " + Unreadz );
      if ( !Unreadz.Equals( "0" ) ) port.Write( "m" );
      else port.Write( "n" );

  } catch ( Exception ee ) { Console.WriteLine( ee.Message ); 
  } finally { System.Threading.Thread.Sleep(1000*60); }
}

EDITED: With working loop.

Should work for it to check every 60 second.

And CheckMail ain't static (that's why it wont compile)


I am sorry mate, your forgetting the basics of the language here. Plus you can have a timer on the main thread and call mail check operation every time on timer elapsed based on the timer count you set. Make sure you lock the mail check method for thread synchronization or else youll run into problems. Or make sure your checkmail operation completes before timer elapses.


ok to fix the problem, i added this code in replacement of the old stuff:

try {
  SerialPort port = new SerialPort( "COM9", 9600, Parity.None, 8, StopBits.One );
  port.Open();

  string Unreadz = "0";
  while ( true ) {
    Unreadz = CheckMail();
    Console.WriteLine( "Unread Mails: " + Unreadz );
    if ( !Unreadz.Equals( "0" ) ) port.Write( "m" );
    else port.Write( "n" );
  }
} catch ( Exception ee ) { Console.WriteLine( ee.Message ); 
} finally { System.Threading.Thread.Sleep(1000*60); }

then i added: "finally { System.Threading.Thread.Sleep(1000*60); }" to:

private string CheckMail() {
        string result = "0";

        try {
            var url = @"https://gmail.google.com/gmail/feed/atom";
            var USER = "username";
            var PASS = "password";

            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;
    }

and to fix the static issue i made " private string CheckMail() {" "private static string CheckMail() {"

Thanks for all the help!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜