最近项目在做一个发送邮件的功能,发现邮件地址列表中存在一个无效的地址,就会全部发送失败
解决方案
Transport transport = session.getTransport(); try { transport.addTransportListener(new CustomTransportListener(transport)); transport.connect("smtp.croot.com",username, password); transport.sendMessage(msg, msg.getAllRecipients()); }catch (Exception e){ e.printStackTrace(); }finally { // 7. 关闭连接 transport.close(); }
在工具类直接写
class CustomTransportListener extends TransportAdapter { Transport transport; CustomTransportListener(Transport transport) { this.transport = transport; } @Override public void messageNotDelivered(TransportEvent transportEvent) { Address[] addresses = transportEvent.getValidUnsentAddresses(); Message message = transportEvent.getMessage(); try { transport.sendMessage(message, addresses); } catch (Exception e) { e.printStackTrace(); } } }