如何使用布尔条件来验证电子邮件(gmail)凭据? [英] How do I use a boolean condition to verify email (gmail) credentials?

查看:105
本文介绍了如何使用布尔条件来验证电子邮件(gmail)凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为编码的新颖性而道歉,我正在尝试制作一个简单的Gmail应用程序,允许用户使用他们的Gmail帐户登录并查看他们是否有任何邮件。我的问题是,当我提示用户登录时,它永远不会验证他们已登录,窗口关闭或我收到Web异常错误

我的程序中有两个类是我遇到的问题。第二个允许使用Gmail.getmail / sendmail(没有问题)

任何帮助将不胜感激!我再次为这样的新手道歉。

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 GmailClient
{
class 计划
{
静态 字符串 userName = ;
static string password = ;

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




}
其他 if false
{

}

< span class =code-comment> // 做事1
// Gmail.getMail(userName,password);



}
else if (selection!= 2
{
// 做事2
Console.WriteLine( 这不是有效的选择);
}


}
while (选择!= 2 );
}

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




public static bool LoggedIn()
{
// 不确定此部分是否有效...
if ((userName == )||(密码== ))
{
return false < /跨度>;
}
else
{

return true ;
}

}
public static int ShowMenu()
{
Console.WriteLine( -------------菜单--------------);
Console.WriteLine( 1)登录gmail);
Console.WriteLine( 2)关闭应用程序);
Console.WriteLine( ----------------- --------------);
Console.WriteLine( 你想做什么?(1/2) );
int userselect = Convert.ToInt32(Console.ReadLine());

if (userselect == 1
{

// 带有第二个菜单
// 要求用户输入他们的Gmail附加
Console.WriteLine( 输入您的Gmail地址:);
userName = Console.ReadLine();


// 询问密码
控制台.WriteLine( 输入你的gmail密码:);
password = Console.ReadLine();






return userselect;

}
else if (userselect!= 2
{
// 退出应用程序不是有效选择
Console.WriteLine( 这不是有效选择);
}


return userselect;
}


public static void LoggedOn()
{
Console.WriteLine( -------------菜单--------------);
Console.WriteLine( 1)登录gmail);
Console.WriteLine( 2)检查新邮件);
Console.WriteLine( 3)发送电子邮件);
Console.WriteLine( 4)关闭应用程序);
Console.WriteLine( ----------------- --------------);
int userselect = Convert.ToInt32(Console.ReadLine());

return ;

// 登录方式
}


public static void GetMail( string newmail)
{
// < span class =code-comment>获取邮件的方法

Gmail.getMail(userName,password);

// newmail = Gmail.getMail(Console.ReadLine));

}


public static void SendMail( string toAddy, string subject, string body)
{
Console.Write(Environment.NewLine + 您想向谁发送电子邮件?);
toAddy = Convert.ToString(Console.ReadLine());
Console.Write(Environment.NewLine + 主题:);
subject = Convert.ToString(Console.ReadLine());
Console.Write(Environment.NewLine + 您的消息:);
body = Convert.ToString(Console.ReadLine());

Gmail.sendMail(userName,password,toAddy,subject,body);
}


}
}











Gmail.cs课程:







 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Xml;
使用 System.Net;
使用 System.Net.Mail;
命名空间 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 密码)
{
System.Net.WebClient objClient = new System.Net.WebClient();
XmlNodeList nodelist;
XmlNode节点;
string 响应;
XmlDocument xmlDoc = new XmlDocument();
Console.WriteLine( 检查邮件......);
objClient.Credentials = new System.Net.NetworkCredential(userName,password);
response = Encoding.UTF8.GetString(objClient.DownloadData( https://mail.google.com /邮件/饲料/原子/));
response = response.Replace( @ < feed version =0.3xmlns = 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
{
电子邮件[ tempCounter] = From: + n.ChildNodes [ 6 ]。InnerText + \ nSubject: + n.FirstChild.InnerText + \ nSummary: + n.ChildNodes [ 1 ]的innerText。
tempCounter ++;
}
}
返回电子邮件;
}
}
}

解决方案

一些例子

< pre lang =java> if (LoggedIn())
{
LoggedOn();
if (true)
{
GetMail();
}



没有多大意义,特别是对于想要了解你的程序的人。更好的想法是这样的:

  if (Login()) //  登录方法返回true或false,表示成功或错误 
{
GetMail( / * 这里可能有一些参数* / );
}
其他
{
// < span class =code-comment>处理你的错误情况
}



你也只需要一个菜单​​来做这么简单的事情,而不是要登录的菜单,然后是另一个具有相同选项的菜单。使用 bool 值来保存将在整个程序中使用的条件(例如,表示用户ID登录的值)。



还有更多的事情需要解决,但需要花费很长时间才能解决所有这些问题。



它会让你写一个简单的设计在开始编码之前解决逻辑问题的论文。想想程序的每个部分需要做的事情。例如:

主要方法应根据用户选择显示菜单和切换逻辑路径:

选项1:登录gmail(获取用户ID和密码后)。

选项2:从Gmail收到邮件(检查您是否已登录)。

选项3:发送邮件。

选项4:终止。



等。


所以这是我到目前为止连续21个小时醒来后所拥有的(目前大约上午5点CST)我完全糊涂了,所以如果有可能告诉我我做错了什么我会非常感激。我觉得我的布尔方法肯定存在不正确之处,以及它是如何实现的。此应用程序的基本前提是让用户使用其凭据登录,检查新电子邮件,发送电子邮件或再次登录,退出。先感谢您。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 GmailClient
{
class 计划
{
静态 字符串 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( 这不是有效的选择。);
}

}

}
其他 if false
{

}

// 做事1
// < span class =code-comment> Gmail.getMail(userName,password);



}
其他 如果(选择!= 2
{
// 做事2
Console.WriteLine( 这不是有效的选择);
}


}
while (选择!= 2 );
}

private static 无效 SendMail()
{

}

public static void CheckMail()
{
// 获取邮件的方法
string [] newmail;
newmail = Gmail.getMail(userName,password);
for int i = 0 ; i < newmail.Length;)
Console.WriteLine(newmail + 以下是您的新电子邮件);

}





public static bool LoggedIn()
{
// 不确定这部分是否有效...
if (( userName == )&&(password == ))
{
return ;
}
else
{

return true ;
}

}
public static int ShowMenu()
{
Console.WriteLine( -------------菜单--------------);
Console.WriteLine( 1)登录gmail);
Console.WriteLine( 2)关闭应用程序);
Console.WriteLine( ----------------- --------------);
Console.WriteLine( 你想做什么?(1/2) );
int userselect = Convert.ToInt32(Console.ReadLine());

if (userselect == 1
{

// 带有第二个菜单
// 要求用户输入他们的Gmail附加
Console.WriteLine( 输入您的Gmail地址:);
userName = Console.ReadLine();


// 询问密码
控制台.WriteLine( 输入你的gmail密码:);
password = Console.ReadLine();

Gmail.Equals(userName,password);




return userselect;

}
else if (userselect!= 2
{
// 退出应用程序不是有效选择
Console.WriteLine( 这不是有效选择);
}


return userselect;
}


public static void LoggedOn( bool @true)
{
Console.WriteLine( -------------菜单--------------< /跨度>);
Console.WriteLine( 1)登录gmail);
Console.WriteLine( 2)检查新邮件);
Console.WriteLine( 3)发送电子邮件);
Console.WriteLine( 4)关闭应用程序);
Console.WriteLine( ----------------- --------------);
int userselect = Convert.ToInt32(Console.ReadLine());

return ;

// 登录方式
}


public static void CheckMail( string [] newmail)
{
// 获取邮件的方法

// newmail = Gmail.getMail(用户名,密码);
// for( int i = 0; i< newmail.Length;)
// Console.WriteLine (newmail +这是您的新电子邮件);





}


< span class =code-keyword> public
static void SendMail( string toAddy, string subject, string body)
{
Console.Write(Environment.NewLine + 您想向谁发送电子邮件? );
toAddy = Convert.ToString(Console.ReadLine());
Console.Write(Environment.NewLine + 主题:);
subject = Convert.ToString(Console.ReadLine());
Console.Write(Environment.NewLine + 您的消息:);
body = Convert.ToString(Console.ReadLine());

Gmail.sendMail(userName,password,toAddy,subject,body);
}


}
}


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.

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:



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

解决方案

Some examples

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:

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.


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.

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


    }
}


这篇关于如何使用布尔条件来验证电子邮件(gmail)凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆