Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
First I apologize for my newness to coding, I am trying to make a simple gmail application that will allow the user to sign in with their gmail account and see if they have any mail. My issue is that when I prompt the user to sign in, it never verifies that they have signed in, either the window closes or I get a web exception error
I have two classes in my program the first is what I'm having an issue with. The second allows the Gmail.getmail/sendmail to be utilized (there's no issues with it)
any help would be greatly appreciated! once again my apologies for being such a novice.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GmailClient
{
    class Program
    {
        static string userName = "";
        static string password = "";

        static void Main(string[] args)
        {
            int selection;
            do
            {
                selection = ShowMenu();
                if (selection == 1)
                {
                    if (LoggedIn())
                    {
                        LoggedOn();
                        if (true)
                        {
                            GetMail();
                        }
                        
                        
                        

                    }
                    else if (false)
                    {

                    }
                    
                    // Do thing 1 
                   // Gmail.getMail(userName, password);

                    
                    
                }
                else if (selection != 2)
                {
                    //Do thing 2 
                    Console.WriteLine("That is not a valid selection");
                }
               

            }
            while (selection != 2);
        }

        private static void GetMail()
        {
            throw new NotImplementedException();
        }




        public static bool LoggedIn()
        {
            // Not sure if this part works...
            if ((userName == "") || (password == ""))
            {
                return false;
            }
            else
            {

                return true;
            }

        }
        public static int ShowMenu()
        {
            Console.WriteLine("-------------Menu--------------");
            Console.WriteLine("1)      Sign into gmail        ");
            Console.WriteLine("2)   Close the application     ");
            Console.WriteLine("-------------------------------");
            Console.WriteLine("What would you like to do? (1/2)");
            int userselect = Convert.ToInt32(Console.ReadLine());

            if (userselect == 1)
            {
                
                // present with second menu 
                // Ask user to enter their Gmail addy 
                Console.WriteLine("Enter your gmail address:");
                userName = Console.ReadLine();


                // Ask for password 
                Console.WriteLine("Enter your gmail password:");
                password = Console.ReadLine();

                
                
                


                return userselect; 

            }
            else if (userselect != 2)
            {
                // Exit application Not a valid selection 
                Console.WriteLine("That is not a valid selection");
            }


            return userselect;
        }


        public static void LoggedOn()
        {
            Console.WriteLine("-------------Menu--------------");
            Console.WriteLine("1)      Sign into gmail        ");
            Console.WriteLine("2)    Check for new Mail       ");
            Console.WriteLine("3)       Send an email         ");
            Console.WriteLine("4)    Close the application    ");
            Console.WriteLine("-------------------------------");
            int userselect = Convert.ToInt32(Console.ReadLine());

            return;

            //Method for being logged on
        }


       public static void GetMail(string newmail)
       {
            // Method for getting Mail 
          Gmail.getMail(userName, password);
          
          // newmail = Gmail.getMail(Console.ReadLine));
          
       }


        public static void SendMail (string toAddy, string subject, string body)
     {
           Console.Write(Environment.NewLine + "Who would you like to send an email to?");
           toAddy = Convert.ToString (Console.ReadLine());
           Console.Write(Environment.NewLine + "Subject: ");
           subject = Convert.ToString(Console.ReadLine());
           Console.Write(Environment.NewLine + "Your Message:");
           body = Convert.ToString(Console.ReadLine());
            
            Gmail.sendMail(userName, password, toAddy, subject, body); 
        }


    }
}





Gmail.cs Class:



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using System.Net.Mail;
namespace GmailClient
{
    class Gmail
    {
        public static void sendMail(string userName, string password, string toAddy, string subject, string body)
        {
            MailAddress fromAddress = new MailAddress(userName);
            MailAddress toAddress = new MailAddress(toAddy);
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(fromAddress.Address, password);
            MailMessage message = new MailMessage(fromAddress, toAddress);
            message.Subject = subject;
            message.Body = body;
            smtp.Send(message);
        }
        public static string[] getMail(string userName, string password)
        {
            System.Net.WebClient objClient = new System.Net.WebClient();
            XmlNodeList nodelist;
            XmlNode node;
            string response;
            XmlDocument xmlDoc = new XmlDocument();
            Console.WriteLine("checking for mail...");
            objClient.Credentials = new System.Net.NetworkCredential(userName, password);
            response = Encoding.UTF8.GetString(objClient.DownloadData("https://mail.google.com/mail/feed/atom/"));
            response = response.Replace(@"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", "<feed>");
            xmlDoc.LoadXml(response);
            node = xmlDoc.SelectSingleNode("/feed/fullcount");
            int mailCount = Convert.ToInt32(node.InnerText);
            string[] emails = new string[0];
            if (mailCount > 0)
            {
                emails = new string[mailCount];
                nodelist = xmlDoc.SelectNodes("/feed/entry");
                node = xmlDoc.SelectSingleNode("title");
                int tempCounter = 0;
                foreach (XmlNode n in nodelist)
                {
                    emails[tempCounter] = "From: " + n.ChildNodes[6].InnerText + "\nSubject: " + n.FirstChild.InnerText + "\nSummary: " + n.ChildNodes[1].InnerText;
                    tempCounter++;
                }
            }
            return emails;
        }
    }
}
Posted
Updated 1-Oct-14 21:03pm
v4
Comments
Sinisa Hajnal 2-Oct-14 2:48am    
Don't apologize for being a novice. This is a fair question. If you asked how to initialize the array or load an image THAT would be something for apology :) Going to work now, I'll check this later.

In the meantime, WHAT error?!
jake2809 2-Oct-14 3:01am    
Well that makes me feel a bit better. As I mentioned the gmail.cs(class) works with zero problems, my issue is now that when the user signs into their account and attempt to check for new mail it never retrieves any mail. Also I'm pretty sure my boolean method is wrong because regardless of what I type it will still populate the secondary menu and then repeat back to the first after an input.
I have changed a bit of the program.cs code(the first part) since I asked this question so I will edit it with the updated code. thank you again!
Richard MacCutchan 2-Oct-14 3:56am    
There is quite a lot of strange logic in here. Your LoggedOn method accepts an option but then throws the value away and returns nothing. You have statements such as if (true), which will of course, like a good wife, always be true. You could simplify your code by reducing the number of redundant methods, and adding proper condition tests where necessary.
jake2809 2-Oct-14 4:18am    
Yes I realize some of it may appear strange, as I stated I am a beginner at this which would most likely be why. If you could clarify what you mean in reference to my "LoggedOn" method I would appreciate it.
Also now under "GetMail()" I am getting an issue saying that its taking 0 arguments after I put this bit of code in:
public static void CheckMail(string[] newmail)
{
// Method for getting Mail
newmail = Gmail.getMail(userName, password);
for (int i = 0; i < newmail.Length; )
Console.WriteLine(newmail +"Here are your new emails");

return;



}
Richard MacCutchan 2-Oct-14 4:52am    
You seem to have trouble understanding the use of parameters and method calls (which are complicated when you start out, as we all did). I would strongly suggest you go to http://docs.oracle.com/javase/tutorial/index.html and study program structure and language before tackling this rather complex application.

See also my suggestions below.

Some examples
Java
if (LoggedIn())
{
    LoggedOn();
    if (true)
    {
        GetMail();
    }

Makes not much sense, especially to someone trying to understand your program. A better idea would be something like:
Java
if (Login()) // the login method returns true or false, indicating success or error
{
    GetMail(/* maybe some parameters here */);
}
else
{
    // handle your error condition
}

You also only need one menu for something this simple, rather than a menu to login, and then another one that has the same option. Use bool values to save conditions which will be used throughout the program (e.g. the value that indicates user id logged in).

There are more things to be addressed but it would take too long to get round to them all.

It would pay you to write a simple design on paper to address the logic issues before you start coding. Think about the things that each part of the program needs to do. For example:
The main method should show a menu and switch logic paths depending on the user choice:
option 1: login to gmail (after getting userid and password).
option 2: get mail from gmail (checking that you are already signed in).
option 3: send a mail message.
option 4: terminate.

etc.
 
Share this answer
 
So this is what I have so far, after being awake for 21 hours straight(currently approx 5AM CST) I am completely befuddled, So if it is all possible to show me what I have done wrong I would greatly greatly appreciate it. I feel that there is definitely something incorrect with my boolean method and how it is implemented. The basic premise of this application is to have the user sign in with their credentials, check for new email, send an email, or sign in again, exit. Thank you in advance.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GmailClient
{
    class Program
    {
        static string userName = "";
        static string password = "";

      public  static void Main(string[] args)
        {
            int selection;
            do
            {
                selection = ShowMenu();
                if (selection == 1)
                {
                    if (LoggedIn())
                    {
                        LoggedOn(true);
                        if (true)
                        {
                            if (selection == 2)
                            {
                                CheckMail();
                            }
                            else if (selection ==3)
                            {
                                SendMail(); 
                            }
                            else if (selection ==4)
                            {
                                Console.WriteLine("That is not a valid selection.");
                            }

                        }
    
                    }
                    else if (false)
                    {

                    }
                    
                    // Do thing 1 
                   // Gmail.getMail(userName, password);

                    
                    
                }
                else if (selection != 2)
                {
                    //Do thing 2 
                    Console.WriteLine("That is not a valid selection");
                }
               

            }
            while (selection != 2);
        }

      private static void SendMail()
      {
         
      }

      public static void CheckMail()
      {
          // Method for getting Mail 
          string[] newmail;  
          newmail = Gmail.getMail(userName, password);
          for (int i = 0; i < newmail.Length; )
              Console.WriteLine(newmail + "Here are your new emails");
         
      }





        public static bool LoggedIn()
        {
            // Not sure if this part works...
            if ((userName == "") && (password == ""))
            {
                return false;
            }
            else
            {

                return true;
            }

        }
        public static int ShowMenu()
        {
            Console.WriteLine("-------------Menu--------------");
            Console.WriteLine("1)      Sign into gmail        ");
            Console.WriteLine("2)   Close the application     ");
            Console.WriteLine("-------------------------------");
            Console.WriteLine("What would you like to do? (1/2)");
            int userselect = Convert.ToInt32(Console.ReadLine());

            if (userselect == 1)
            {
                
                // present with second menu 
                // Ask user to enter their Gmail addy 
                Console.WriteLine("Enter your gmail address:");
                userName = Console.ReadLine();


                // Ask for password 
                Console.WriteLine("Enter your gmail password:");
                password = Console.ReadLine();

                Gmail.Equals(userName, password); 
                
                


                return userselect; 

            }
            else if (userselect != 2)
            {
                // Exit application Not a valid selection 
                Console.WriteLine("That is not a valid selection");
            }


            return userselect;
        }


        public static void LoggedOn(bool @true)
        {
            Console.WriteLine("-------------Menu--------------");
            Console.WriteLine("1)      Sign into gmail        ");
            Console.WriteLine("2)    Check for new Mail       ");
            Console.WriteLine("3)       Send an email         ");
            Console.WriteLine("4)    Close the application    ");
            Console.WriteLine("-------------------------------");
            int userselect = Convert.ToInt32(Console.ReadLine());

            return;

            //Method for being logged on
        }


        public static void CheckMail(string[] newmail)
       {
            // Method for getting Mail 
          
         // newmail = Gmail.getMail(userName, password);
         // for (int i = 0; i < newmail.Length; )
          //    Console.WriteLine(newmail +"Here are your new emails");
         
             
  
          
          
       }


       public static void SendMail(string toAddy, string subject, string body)
     {
           Console.Write(Environment.NewLine + "Who would you like to send an email to?");
           toAddy = Convert.ToString (Console.ReadLine());
           Console.Write(Environment.NewLine + "Subject: ");
           subject = Convert.ToString(Console.ReadLine());
           Console.Write(Environment.NewLine + "Your Message:");
           body = Convert.ToString(Console.ReadLine());
            
            Gmail.sendMail(userName, password, toAddy, subject, body); 
        }


    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900