2011-12-07 4 views
0

글쎄, 2 개의 EditTexts에 Gmail 주소와 패스를 넣었고 OK 버튼을 클릭하면 자바 메일 API로 Gmail 계정에 메일을 보내려고합니다. 하지만 잘못된 데이터를 입력하면 인증되지 않으므로 이메일을 보내지 않습니다. 응용 프로그램을 다시 시작하고 실제 데이터를 사용하면 다시 인증에 실패합니다. 내가 잘못한 일을하거나 자바 메일 API로 처리 한 방법입니까? 여기에 전자 메일 보내기 위해 내가 사용하는 것입니다 : 내가 로그 캣에서 무엇을 얻을 여기두 번째로 인증 실패 Java Mail 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"); 
                   } 

} 

} 을

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의 일종이며, 그것은 ObjectString에서 매핑합니다. 동일한 키를 두 번 연속 사용하여 put 메서드를 호출하면 두 번째 호출 의 값이 첫 번째 호출에서 추가 한 값인을 바꿉니다.

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

당신의 "465"항목이 무시됩니다

따라서 위는 동일합니다.

+0

네가 맞다.하지만 465 포트만 사용하면 같은 오류가 발생한다! – dothedos