2011-12-07 66 views
0

嗯,我把我的Gmail地址和我的通行證放在2個EditTexts中,當我點擊確定按鈕時,我嘗試用我的Gmail郵件api發送郵件。但如果我把錯誤的數據,它不認證,因此我不會發送電子郵件。如果我再次啓動應用程序並使用真實數據,則無法再次進行身份驗證。我做錯了什麼或者是用java mail api完成的事情嗎?下面是我用什麼方法發送電子郵件:第二次身份驗證失敗Java郵件API

public GmailSender(String user, String password) { 
    this.user = user; 
    this.password = password; 

    Properties props = new Properties(); 
    props.setProperty("mail.transport.protocol", "smtp"); 
    props.setProperty("mail.host", mailhost); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.port", "587"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.port", "587"); 
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 
    props.setProperty("mail.smtp.quitwait", "false"); 

    session = Session.getDefaultInstance(props, this); 

} 

protected PasswordAuthentication getPasswordAuthentication() { 

    return new PasswordAuthentication(user, password); 
} 
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { 

    MimeMessage message = new MimeMessage(session); 
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); 
    message.setSender(new InternetAddress(sender)); 
    message.setSubject(subject); 
    message.setDataHandler(handler); 
    if (recipients.indexOf(',') > 0) 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); 
    else 
     message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); 
    Transport.send(message); 
} 

public class ByteArrayDataSource implements DataSource { 
    private byte[] data; 
    private String type; 

    public ByteArrayDataSource(byte[] data, String type) { 
     super(); 
     this.data = data; 
     this.type = type; 
                  } 

    public ByteArrayDataSource(byte[] data) { 
     super(); 
     this.data = data; 
              } 

    public void setType(String type) { 
     this.type = type; 
             } 

    public String getContentType() { 
     if (type == null) 
      return "application/octet-stream"; 
     else 
      return type; 
            } 

    public InputStream getInputStream() throws IOException { 
     return new ByteArrayInputStream(data); 
                  } 

    public String getName() { 
     return "ByteArrayDataSource"; 
          } 

    public OutputStream getOutputStream() throws IOException { 
     throw new IOException("Not Supported"); 
                   } 

} 

} 這裏是我從logcat中得到

12-07 09:58:50.781: E/Validate(595): println needs a message 
12-07 09:58:50.781: E/Validate(595): java.lang.NullPointerException: println needs a  message 
12-07 09:58:50.781: E/Validate(595): at android.util.Log.println_native(Native Method) 
12-07 09:58:50.781: E/Validate(595): at android.util.Log.i(Log.java:143) 
12-07 09:58:50.781: E/Validate(595): at  stathis.example.teliko.Data.onClick(Data.java:107) 
12-07 09:58:50.781: E/Validate(595): at  android.view.View.performClick(View.java:2408) 
12-07 09:58:50.781: E/Validate(595): at android.view.View$PerformClick.run(View.java:8816) 
12-07 09:58:50.781: E/Validate(595): at android.os.Handler.handleCallback(Handler.java:587) 
12-07 09:58:50.781: E/Validate(595): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-07 09:58:50.781: E/Validate(595): at android.os.Looper.loop(Looper.java:123) 
12-07 09:58:50.781: E/Validate(595): at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-07 09:58:50.781: E/Validate(595): at java.lang.reflect.Method.invokeNative(Native Method) 
12-07 09:58:50.781: E/Validate(595): at java.lang.reflect.Method.invoke(Method.java:521) 
12-07 09:58:50.781: E/Validate(595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-07 09:58:50.781: E/Validate(595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-07 09:58:50.781: E/Validate(595): at dalvik.system.NativeStart.main(Native Method) 
+0

來自服務器的迴應。 – hovanessyan

+0

當然,但我該如何做到? – dothedos

+0

你在logcat中有一些輸出嗎? – hovanessyan

回答

1

我不知道這是不是你的問題,但下面的代碼是錯誤的:

Properties props = new Properties(); 
... 
props.put("mail.smtp.port", "465"); 
props.put("mail.smtp.port", "587"); 
props.put("mail.smtp.socketFactory.port", "465"); 
props.put("mail.smtp.socketFactory.port", "587"); 

Properties對象是一種Map,它從一個String映射到Object。如果您使用同一個密鑰連續撥打put方法兩次,則第二個呼叫中的值將替換您在第一個呼叫中添加的值

所以上面的等價於:

Properties props = new Properties(); 
... 
props.put("mail.smtp.port", "587"); 
props.put("mail.smtp.socketFactory.port", "587"); 

你的 「465」 項目將被忽略。

+0

那麼你是對的,但如果我使用465端口只有它出現了相同的錯誤! – dothedos