【问题标题】:Sending mail from Android app gives "less secure app" error从 Android 应用程序发送邮件会出现“不太安全的应用程序”错误
【发布时间】:2019-01-19 21:36:05
【问题描述】:

我使用 Android Studio 和这个库; https://github.com/yesidlazaro/GmailBackground

我正在尝试从我的 android 应用程序发送电子邮件。但是每当我尝试从 Gmail 帐户发送电子邮件时,它都不起作用。因为谷歌将我的应用固定为“不太安全的应用”并阻止它。

我不得不从 gmail 的安全设置中允许不太安全的应用程序。但我不想这样做。我怎么解决这个问题?让我的应用程序变得不那么安全? 谢谢。

【问题讨论】:

    标签: android email gmail


    【解决方案1】:
    1. 下载 jar 文件并粘贴到您的 libs 文件夹中:link to download

    2. 创建一个SendMail.java

       public class SendMail extends AsyncTask<Void,Void,Void> {
       private Context context;
       private Session session;
      
      //Information to send email
      private String email;
      private String subject;
      private String message;
      
      private static int SPLASH_TIME_OUT = 3000;
      //Send button
      
      
      //Progressdialog to show while sending email
      private ProgressDialog progressDialog;
      
      //Class Constructor
      public SendMail(Context context, String email, String subject, String message){
          //Initializing variables
          this.context = context;
          this.email = email;
          this.subject = subject;
          this.message = message;
      }
      
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          //Showing progress dialog while sending email
          progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
      }
      
      @Override
      protected void onPostExecute(Void aVoid) {
          super.onPostExecute(aVoid);
          //Dismissing the progress dialog
          progressDialog.dismiss();
          //Showing a success message
          Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show();
      
      
      
      }
      
      @Override
      protected Void doInBackground(Void... params) {
          //Creating properties
          Properties props = new Properties();
      
          //Configuring properties for gmail
          //If you are not using gmail you may need to change the values
          props.put("mail.smtp.host", "smtp.gmail.com");
          props.put("mail.smtp.socketFactory.port", "465");
          props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.port", "465");
      
          //Creating a new session
          session = Session.getDefaultInstance(props,
                  new javax.mail.Authenticator() {
                      //Authenticating the password
                      protected PasswordAuthentication getPasswordAuthentication() {
                          return new PasswordAuthentication("abc@gmail.com", "password");
                      }
                  });
      
          try {
              //Creating MimeMessage object
              MimeMessage mm = new MimeMessage(session);
      
              //Setting sender address
              mm.setFrom(new InternetAddress("abc@gmail.com"));
              //Adding receiver
              mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
              //Adding subject
              mm.setSubject(subject);
              //Adding message
              mm.setText(message);
      
              //Sending email
              Transport.send(mm);
      
          } catch (MessagingException e) {
              e.printStackTrace();
          }
          return null;
      }} 
      
    3. 然后在你的 MainActivity.java 中:

          //Creating SendMail object
          SendMail sm = new SendMail(this, email_id, fullname, message);
      
          //Executing sendmail to send email
          sm.execute();
      
    4. 使用此link启用不太安全的应用程序

    【讨论】:

    • 我可以在不启用安全性较低的应用程序的情况下执行此任务吗?
    • 我认为 Google 不会允许这样做。我也尝试过不启用但没有奏效。但启用后它对我有用。
    【解决方案2】:

    请按步骤操作

    1.转到 gmail 2.设置 3.安全 4.启用不太安全的应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-19
      • 2020-04-17
      • 2023-03-30
      • 2020-01-07
      • 2011-08-06
      • 1970-01-01
      相关资源
      最近更新 更多