好吧,我将我的 gmail 地址和通行证放入 2 个 EditTexts 中,当我单击“确定”按钮时,我尝试使用 java mail api 在我的 gmail 帐户中发送邮件。但如果我输入了错误的数据,它不会进行身份验证,因此我不会发送电子邮件。如果我再次启动应用程序并使用真实数据,则它无法再次进行身份验证。我做错了什么还是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");
}
}
} 这是我从 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)
最佳答案
我不知道这是否是你的问题,但以下代码是错误的:
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”条目将被忽略。
关于java - 第二次身份验证失败 Java Mail API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8413315/