您如何使用 JavaMail API 侦听身份验证错误?
How do you listen for authentication errors using JavaMail API?
我正在编写一个简单的 javamail 客户端来从帐户发送电子邮件。我想监听身份验证失败,以便显示适当的消息。我正在尝试使用 Transport 对象并向其添加连接侦听器。我可以在控制台中看到我收到错误,但我分配给 "addConnectionListener()" 的对象没有拾取它。示例代码:
mSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(CustomMailSender.this.mUser, CustomMailSender.this.mPassword);
}
});
mTransport = mSession.getTransport(protocol);
mTransport.addConnectionListener(new ConnectionListener() {
@Override
public void opened(ConnectionEvent e) {
Timber.e(" connection opened");
}
@Override
public void disconnected(ConnectionEvent e) {
Timber.e(" connection disconnected");
}
@Override
public void closed(ConnectionEvent e) {
Timber.e(" connection closed");
}
});
int iPort = Integer.parseInt(port);
mTransport.connect(mMailHost,iPort , user, password);
我在这里发消息
mTransport.sendMessage(message, InternetAddress.parse(recipients));
我曾预计如果连接尝试失败,"disconnected" 甚至会触发 ?
在 JavaMail 中,"connected" 实际上意味着 "connected and authenticated and ready for use"。如果身份验证失败,则传输永远不会 "connected".
我不确定为什么您需要 "listen" 验证失败,因为当连接方法抛出 AuthenticationFailedException 时,它们会被同步报告。
我正在编写一个简单的 javamail 客户端来从帐户发送电子邮件。我想监听身份验证失败,以便显示适当的消息。我正在尝试使用 Transport 对象并向其添加连接侦听器。我可以在控制台中看到我收到错误,但我分配给 "addConnectionListener()" 的对象没有拾取它。示例代码:
mSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(CustomMailSender.this.mUser, CustomMailSender.this.mPassword);
}
});
mTransport = mSession.getTransport(protocol);
mTransport.addConnectionListener(new ConnectionListener() {
@Override
public void opened(ConnectionEvent e) {
Timber.e(" connection opened");
}
@Override
public void disconnected(ConnectionEvent e) {
Timber.e(" connection disconnected");
}
@Override
public void closed(ConnectionEvent e) {
Timber.e(" connection closed");
}
});
int iPort = Integer.parseInt(port);
mTransport.connect(mMailHost,iPort , user, password);
我在这里发消息
mTransport.sendMessage(message, InternetAddress.parse(recipients));
我曾预计如果连接尝试失败,"disconnected" 甚至会触发 ?
在 JavaMail 中,"connected" 实际上意味着 "connected and authenticated and ready for use"。如果身份验证失败,则传输永远不会 "connected".
我不确定为什么您需要 "listen" 验证失败,因为当连接方法抛出 AuthenticationFailedException 时,它们会被同步报告。