我已经开始使用JAVA邮件API实现邮件模块。我使用SMTPMessage,以便我可以获得邮件的传递状态。问题是我正在获得wrong mail addresses
这是我发送消息所做的工作
SMTPMessage smtpMsg=new SMTPMessage(message);
smtpMsg.setReturnOption(SMTPMessage.RETURN_HDRS);
smtpMsg.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS |SMTPMessage.NOTIFY_FAILURE);
Transport transport = session.getTransport("smtp");
//attach the listeners for the connection and transmission
transport.addConnectionListener(this);
transport.addTransportListener(this);
transport.connect();
//connect to the server and send the message
transport.sendMessage(smtpMsg, smtpMsg.getAllRecipients());
由于我的类实现了TransportListener,ConnectionListener
,我将侦听器附加为
public void opened(ConnectionEvent e) {
System.out.println("Connection opened");
}
public void disconnected(ConnectionEvent e) {
System.out.println("Connection disconnected");
}
public void closed(ConnectionEvent e) {
System.out.println("Connection closed");
}
public void messageDelivered(TransportEvent e) {
System.out.println("Message delivered for:");
if (e != null) {
Address[] a = e.getValidSentAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a[i]).getAddress());
}
}
System.out.println("");
}
}
public void messageNotDelivered(TransportEvent e) {
System.out.println("Message not delivered for:");
if (e != null) {
Address[] a = e.getValidUnsentAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a[i]).getAddress());
}
}
System.out.println("");
}
}
public void messagePartiallyDelivered(TransportEvent e) {
System.out.println("These addresses are invalid:");
if (e != null) {
Address[] a = e.getInvalidAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a[i]).getAddress());
}
}
System.out.println("");
}
}
这是在博客中提出的。这里有什么不对?如何将错误邮件IDS的传递状态设置为false
这就是我初始化消息的方式
MimeMessage message=new MimeMessage(session);
//set the mail sender address
message.setFrom(new InternetAddress(userName));
//set the recipient addresses
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
//set subject of the mail
message.setSubject(subject);
//Set the body of the message
message.setText(body);