List of usage examples for javax.mail SendFailedException getInvalidAddresses
public Address[] getInvalidAddresses()
From source file:msgsendsample.java
public static void main(String[] args) { if (args.length != 4) { usage();/*from ww w. j av a2 s. c o m*/ System.exit(1); } System.out.println(); String to = args[0]; String from = args[1]; String host = args[2]; boolean debug = Boolean.valueOf(args[3]).booleanValue(); // create some properties and get the default Session Properties props = new Properties(); props.put("mail.smtp.host", host); if (debug) props.put("mail.debug", args[3]); Session session = Session.getInstance(props, null); session.setDebug(debug); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("JavaMail APIs Test"); msg.setSentDate(new Date()); // If the desired charset is known, you can use // setText(text, charset) msg.setText(msgText); Transport.send(msg); } catch (MessagingException mex) { System.out.println("\n--Exception handling in msgsendsample.java"); mex.printStackTrace(); System.out.println(); Exception ex = mex; do { if (ex instanceof SendFailedException) { SendFailedException sfex = (SendFailedException) ex; Address[] invalid = sfex.getInvalidAddresses(); if (invalid != null) { System.out.println(" ** Invalid Addresses"); for (int i = 0; i < invalid.length; i++) System.out.println(" " + invalid[i]); } Address[] validUnsent = sfex.getValidUnsentAddresses(); if (validUnsent != null) { System.out.println(" ** ValidUnsent Addresses"); for (int i = 0; i < validUnsent.length; i++) System.out.println(" " + validUnsent[i]); } Address[] validSent = sfex.getValidSentAddresses(); if (validSent != null) { System.out.println(" ** ValidSent Addresses"); for (int i = 0; i < validSent.length; i++) System.out.println(" " + validSent[i]); } } System.out.println(); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:MainClass.java
public static void main(String[] args) { if (args.length != 4) { usage();/*from w w w . ja va 2 s.c o m*/ System.exit(1); } System.out.println(); String to = args[0]; String from = args[1]; String host = args[2]; boolean debug = Boolean.valueOf(args[3]).booleanValue(); // create some properties and get the default Session Properties props = new Properties(); props.put("mail.smtp.host", host); if (debug) props.put("mail.debug", args[3]); Session session = Session.getInstance(props, null); session.setDebug(debug); try { // create a message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(args[0]) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("JavaMail APIs Test"); msg.setSentDate(new Date()); // If the desired charset is known, you can use // setText(text, charset) msg.setText(msgText); Transport.send(msg); } catch (MessagingException mex) { System.out.println("\n--Exception handling in msgsendsample.java"); mex.printStackTrace(); System.out.println(); Exception ex = mex; do { if (ex instanceof SendFailedException) { SendFailedException sfex = (SendFailedException) ex; Address[] invalid = sfex.getInvalidAddresses(); if (invalid != null) { System.out.println(" ** Invalid Addresses"); if (invalid != null) { for (int i = 0; i < invalid.length; i++) System.out.println(" " + invalid[i]); } } Address[] validUnsent = sfex.getValidUnsentAddresses(); if (validUnsent != null) { System.out.println(" ** ValidUnsent Addresses"); if (validUnsent != null) { for (int i = 0; i < validUnsent.length; i++) System.out.println(" " + validUnsent[i]); } } Address[] validSent = sfex.getValidSentAddresses(); if (validSent != null) { System.out.println(" ** ValidSent Addresses"); if (validSent != null) { for (int i = 0; i < validSent.length; i++) System.out.println(" " + validSent[i]); } } } System.out.println(); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:transport.java
public void go(Session session, InternetAddress[] toAddr, InternetAddress from) { Transport trans = null;/* ww w . java2 s .com*/ try { // create a message Message msg = new MimeMessage(session); msg.setFrom(from); msg.setRecipients(Message.RecipientType.TO, toAddr); msg.setSubject("JavaMail APIs transport.java Test"); msg.setSentDate(new Date()); // Date: header msg.setContent(msgText + msgText2, "text/plain"); msg.saveChanges(); // get the smtp transport for the address trans = session.getTransport(toAddr[0]); // register ourselves as listener for ConnectionEvents // and TransportEvents trans.addConnectionListener(this); trans.addTransportListener(this); // connect the transport trans.connect(); // send the message trans.sendMessage(msg, toAddr); // give the EventQueue enough time to fire its events try { Thread.sleep(5); } catch (InterruptedException e) { } } catch (MessagingException mex) { // give the EventQueue enough time to fire its events try { Thread.sleep(5); } catch (InterruptedException e) { } System.out.println("Sending failed with exception:"); mex.printStackTrace(); System.out.println(); Exception ex = mex; do { if (ex instanceof SendFailedException) { SendFailedException sfex = (SendFailedException) ex; Address[] invalid = sfex.getInvalidAddresses(); if (invalid != null) { System.out.println(" ** Invalid Addresses"); for (int i = 0; i < invalid.length; i++) System.out.println(" " + invalid[i]); } Address[] validUnsent = sfex.getValidUnsentAddresses(); if (validUnsent != null) { System.out.println(" ** ValidUnsent Addresses"); for (int i = 0; i < validUnsent.length; i++) System.out.println(" " + validUnsent[i]); } Address[] validSent = sfex.getValidSentAddresses(); if (validSent != null) { System.out.println(" ** ValidSent Addresses"); for (int i = 0; i < validSent.length; i++) System.out.println(" " + validSent[i]); } } System.out.println(); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } finally { try { // close the transport if (trans != null) trans.close(); } catch (MessagingException mex) { /* ignore */ } } }
From source file:transport.java
public void go(Session session, InternetAddress[] toAddr, InternetAddress from) { Transport trans = null;//from ww w . java 2 s. co m try { // create a message Message msg = new MimeMessage(session); msg.setFrom(from); msg.setRecipients(Message.RecipientType.TO, toAddr); msg.setSubject("JavaMail APIs transport.java Test"); msg.setSentDate(new Date()); // Date: header msg.setContent(msgText + msgText2, "text/plain"); msg.saveChanges(); // get the smtp transport for the address trans = session.getTransport(toAddr[0]); // register ourselves as listener for ConnectionEvents // and TransportEvents trans.addConnectionListener(this); trans.addTransportListener(this); // connect the transport trans.connect(); // send the message trans.sendMessage(msg, toAddr); // give the EventQueue enough time to fire its events try { Thread.sleep(5); } catch (InterruptedException e) { } } catch (MessagingException mex) { // give the EventQueue enough time to fire its events try { Thread.sleep(5); } catch (InterruptedException e) { } mex.printStackTrace(); System.out.println(); Exception ex = mex; do { if (ex instanceof SendFailedException) { SendFailedException sfex = (SendFailedException) ex; Address[] invalid = sfex.getInvalidAddresses(); if (invalid != null) { System.out.println(" ** Invalid Addresses"); if (invalid != null) { for (int i = 0; i < invalid.length; i++) System.out.println(" " + invalid[i]); } } Address[] validUnsent = sfex.getValidUnsentAddresses(); if (validUnsent != null) { System.out.println(" ** ValidUnsent Addresses"); if (validUnsent != null) { for (int i = 0; i < validUnsent.length; i++) System.out.println(" " + validUnsent[i]); } } Address[] validSent = sfex.getValidSentAddresses(); if (validSent != null) { System.out.println(" ** ValidSent Addresses"); if (validSent != null) { for (int i = 0; i < validSent.length; i++) System.out.println(" " + validSent[i]); } } } System.out.println(); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } finally { try { // close the transport trans.close(); } catch (MessagingException mex) { /* ignore */ } } }
From source file:org.appverse.web.framework.backend.api.services.integration.impl.live.MailIntegrationServiceImpl.java
@Override @SuppressWarnings("unchecked") public void sendMail(String from, String[] to, String[] copyTo, String subject, String templateLocation, Map model) throws Exception { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from);/*w w w. j ava 2s.c om*/ helper.setTo(to); if (copyTo != null) { helper.setCc(copyTo); } helper.setSubject(subject); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, ENCODING, templateLocation, model); helper.setText(text, true); try { this.mailSender.send(message); } catch (MailSendException sendMailException) { Exception[] messageExceptions = sendMailException.getMessageExceptions(); for (Exception messageException : messageExceptions) { if (messageException instanceof SendFailedException) { SendFailedException sendFailedException = (SendFailedException) messageException; if (sendFailedException.getMessage().equals(INVALID_ADDRESSES)) { Address[] invalidAddresses = sendFailedException.getInvalidAddresses(); List<String> invalidAddressList = new ArrayList<String>(); for (Address invalidAddress : invalidAddresses) { String invalidAddressString = invalidAddress.toString(); invalidAddressList.add(invalidAddressString); } List<String> validToAdressesList = new ArrayList<String>(); if (to != null && to.length > 0) { for (String address : to) { if (!invalidAddressList.contains(address)) { validToAdressesList.add(address); } } } List<String> validCopyToAdressesList = new ArrayList<String>(); if (copyTo != null && copyTo.length > 0) { for (String address : copyTo) { if (!invalidAddressList.contains(address)) { validCopyToAdressesList.add(address); } } } String[] validToAdressesArray = new String[validToAdressesList.size()]; validToAdressesList.toArray(validToAdressesArray); helper.setTo(validToAdressesList.toArray(validToAdressesArray)); String[] validCopyToAdressesArray = new String[validCopyToAdressesList.size()]; validCopyToAdressesList.toArray(validCopyToAdressesArray); helper.setCc(validCopyToAdressesList.toArray(validCopyToAdressesArray)); for (String invalidAddress : invalidAddressList) { logger.error("Mail not sended to " + invalidAddress + "due its a invalid address"); } this.mailSender.send(message); } } } } }
From source file:org.appverse.web.framework.backend.api.services.integration.impl.live.MailIntegrationServiceImpl.java
@SuppressWarnings("unchecked") @Override/*from w w w . ja v a2 s . com*/ public void sendMail(String from, String[] to, String[] copyTo, String subject, String templateLocation, Map model, ArrayList<AttachmentDTO> attachments) throws Exception { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); if (copyTo != null) { helper.setCc(copyTo); } helper.setSubject(subject); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, ENCODING, templateLocation, model); helper.setText(text, true); for (AttachmentDTO attachmentDTO : attachments) { helper.addAttachment(attachmentDTO.getAttachmentName(), attachmentDTO.getAttachment()); } try { this.mailSender.send(message); } catch (MailSendException sendMailException) { Exception[] messageExceptions = sendMailException.getMessageExceptions(); for (Exception messageException : messageExceptions) { if (messageException instanceof SendFailedException) { SendFailedException sendFailedException = (SendFailedException) messageException; if (sendFailedException.getMessage().equals(INVALID_ADDRESSES)) { Address[] invalidAddresses = sendFailedException.getInvalidAddresses(); List<String> invalidAddressList = new ArrayList<String>(); for (Address invalidAddress : invalidAddresses) { String invalidAddressString = invalidAddress.toString(); invalidAddressList.add(invalidAddressString); } List<String> validToAdressesList = new ArrayList<String>(); if (to != null && to.length > 0) { for (String address : to) { if (!invalidAddressList.contains(address)) { validToAdressesList.add(address); } } } List<String> validCopyToAdressesList = new ArrayList<String>(); if (copyTo != null && copyTo.length > 0) { for (String address : copyTo) { if (!invalidAddressList.contains(address)) { validCopyToAdressesList.add(address); } } } String[] validToAdressesArray = new String[validToAdressesList.size()]; validToAdressesList.toArray(validToAdressesArray); helper.setTo(validToAdressesList.toArray(validToAdressesArray)); String[] validCopyToAdressesArray = new String[validCopyToAdressesList.size()]; validCopyToAdressesList.toArray(validCopyToAdressesArray); helper.setCc(validCopyToAdressesList.toArray(validCopyToAdressesArray)); for (String invalidAddress : invalidAddressList) { logger.error("Mail not sended to " + invalidAddress + "due its a invalid address"); } this.mailSender.send(message); } } } } }
From source file:hornet.framework.mail.MailServiceImpl.java
/** * Converti l'exception fourni en {@link BusinessException}. * * @param mse//from www.j av a2 s .co m * MailSendException * @return Une {@link BusinessException} */ protected BusinessException toBusinessException(final MailSendException mse) { MailServiceImpl.LOG.debug("Conversion d'une erreur d'envoi de courriel"); final List<String> adressesInvalides = new ArrayList<String>(); for (final Exception exception : mse.getMessageExceptions()) { if (exception instanceof SendFailedException) { final SendFailedException sendFailedEx = (SendFailedException) exception; for (final Address adresseInvalide : sendFailedEx.getInvalidAddresses()) { adressesInvalides.add(adresseInvalide.toString()); MailServiceImpl.LOG.debug(String.format("adresse invalide : %s", adresseInvalide)); } } else if (exception instanceof AuthenticationFailedException) { final AuthenticationFailedException authenticationFailedEx = (AuthenticationFailedException) exception; return new BusinessException("erreur.envoi.courriel.authentification", authenticationFailedEx); } } if (adressesInvalides.isEmpty()) { return new BusinessException("erreur.envoi.courriel", mse); } else { return new BusinessException("erreur.envoi.courriel.addressesInvalides", new String[] { StringUtils.join(adressesInvalides, ", ") }, mse); } }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void mailFromError() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250 OK").recvLine() // MAIL FROM .sendLine("451 error").recvLine() // QUIT .build().start(PORT);/*from ww w .j a v a 2s . c o m*/ Session session = JMSession.getSession(); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, null, null); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\n" + "Subject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); try { transport.sendMessage(msg, msg.getAllRecipients()); Assert.fail(); } catch (SendFailedException e) { Assert.assertEquals(0, e.getValidSentAddresses().length); Assert.assertEquals(0, e.getValidUnsentAddresses().length); Assert.assertEquals(0, e.getInvalidAddresses().length); } finally { transport.close(); } server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void rcptToError() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250 OK").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("550 error").recvLine() // QUIT .build().start(PORT);/*w w w . j a v a 2s . c o m*/ Session session = JMSession.getSession(); session.getProperties().setProperty("mail.smtp.sendpartial", "true"); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, null, null); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); try { transport.sendMessage(msg, msg.getAllRecipients()); Assert.fail(); } catch (SendFailedException e) { Assert.assertEquals(0, e.getValidSentAddresses().length); Assert.assertEquals(0, e.getValidUnsentAddresses().length); Assert.assertEquals(1, e.getInvalidAddresses().length); } finally { transport.close(); } server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void dataError() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250 OK").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("250 OK").recvLine() // DATA .sendLine("451 error").recvLine() // QUIT .build().start(PORT);/*from w w w.j a v a2s. c o m*/ Session session = JMSession.getSession(); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, null, null); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); try { transport.sendMessage(msg, msg.getAllRecipients()); Assert.fail(); } catch (SendFailedException e) { Assert.assertEquals(1, e.getValidSentAddresses().length); Assert.assertEquals(0, e.getValidUnsentAddresses().length); Assert.assertEquals(0, e.getInvalidAddresses().length); } finally { transport.close(); } server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay()); Assert.assertEquals("DATA\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }