2014-08-30 139 views
0

我想從一個應用程序發送的電子郵件,我使用下面的代碼:發送郵件:驗證失敗

private static final String username = "[email protected]"; 
private static final String password = "pass"; 


private void sendMail(String email, String subject, String messageBody) { 
     Session session = createSessionObject(); 

     try { 
      Message message = createMessage(email, subject, messageBody, session); 
      new SendMailTask().execute(message); 
     } catch (AddressException e) { 
      e.printStackTrace(); 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
    } 

    private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException { 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email)); 
     message.setSubject(subject); 
     message.setText(messageBody); 
     return message; 
    } 

    private Session createSessionObject() { 
     Properties properties = new Properties(); 
     properties.put("mail.smtp.auth", "true"); 
     properties.put("mail.smtp.starttls.enable", "true"); 
     properties.put("mail.smtp.host", "smtp.gmail.com"); 
     properties.put("mail.smtp.port", "587"); 

     return Session.getInstance(properties, new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
     }); 
    } 

    private class SendMailTask extends AsyncTask<Message, Void, Void> { 
     private ProgressDialog progressDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      progressDialog = ProgressDialog.show(SendQuestion.this, "Please wait", "Sending mail", true, false); 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 
      progressDialog.dismiss(); 
     } 

     @Override 
     protected Void doInBackground(Message... messages) { 
      try { 
       Transport.send(messages[0]); 
      } catch (MessagingException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    } 

的Eclipse拋出javax.mail.AuthenticationFailedException。當我第一次嘗試發送郵件時,我的Gmail帳戶收到了谷歌有關安全的郵件,並且他們阻止了郵件。然後我激活/ lesssecureapps,我想允許不太安全的應用程序。但是這並沒有解決它。我的代碼有問題嗎?或者有gmail問題?

PS:我已經添加了所有必需的.jar和包括互聯網接入體現

回答

0

因爲它事實證明,這是谷歌的服務器的問題。我與支持聯繫,他們修好了。上面的代碼工作得很好。

0

試着這樣做:

public static int Email(String fromemail, String toemail, String cc, 
      String bcc, String Subject, String Body, String Attachment) { 
     final String fromEmail = fromemail;// "[email protected]"; //requires 
     // valid gmail id 
     // final String password = Password; // correct password for gmail id 
     final String toEmail = toemail;// "[email protected]"; // can be any 
     // email id 

     MailSSLSocketFactory sf = null; 
     try { 
      sf = new MailSSLSocketFactory(); 
     } catch (GeneralSecurityException e) { 
      e.printStackTrace(); 
     } 
     sf.setTrustAllHosts(true); 

     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); // SMTP Host 
     props.put("mail.smtp.socketFactory.port", "465"); // SSL Port 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); // SSL Factory Class 
     props.put("mail.smtp.auth", "true"); // Enabling SMTP Authentication 
     props.put("mail.smtp.port", "465"); // SMTP Port 

     Authenticator auth = new Authenticator() { 
      // override the getPasswordAuthentication method 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(fromEmail, Password); 
      } 
     }; 

     Session session = Session.getDefaultInstance(props, auth); 

     int i; 
     i = sendEmail(session, toEmail, Subject, Body); 
     return i; 

    } 

這是發送功能:

public static void sendEmail(Session session, String toEmail, 
      String subject, String body) { 
     try { 
      MimeMessage msg = new MimeMessage(session); 
      // set message headers 
      msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); 
      msg.addHeader("format", "flowed"); 
      msg.addHeader("Content-Transfer-Encoding", "8bit"); 

      msg.setFrom(new InternetAddress("[email protected]", 
        "Operation Department Email")); 

      msg.setReplyTo(InternetAddress.parse("[email protected]", 
        false)); 

      msg.setSubject(subject, "UTF-8"); 

      msg.setText(body, "UTF-8"); 

      msg.setSentDate(new Date()); 

      msg.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse(toEmail, false)); 
      Transport.send(msg); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

謝謝你的回答。 2件東西: 1)i = sendEmail(session,toEmail,Subject,Body); sendEmail無效,所以這是錯誤 2)對於MailSSLSocketFactory我應該創建新的類嗎? – 2014-08-30 09:54:51