List of usage examples for javax.mail SendFailedException getMessage
public String getMessage()
From source file:com.liferay.util.mail.MailEngine.java
public static void send(byte[] msgByteArray) throws MailEngineException { InternetAddress[] from = null;/* www .j a v a 2 s. c o m*/ try { Session session = getSession(); Message msg = new MimeMessage(session, new ByteArrayInputStream(msgByteArray)); from = (InternetAddress[]) msg.getFrom(); _sendMessage(session, msg); } catch (SendFailedException sfe) { Logger.error(MailEngine.class, sfe.getMessage(), sfe); } catch (Exception e) { throw new MailEngineException(e); } }
From source file:com.liferay.util.mail.MailEngine.java
public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, String subject, String body, boolean htmlFormat) throws MailEngineException { long start = System.currentTimeMillis(); try {/*from w w w .j a v a 2 s . c om*/ Session session = getSession(); Message msg = new MimeMessage(session); msg.setFrom(from); msg.setRecipients(Message.RecipientType.TO, to); if (cc != null) { msg.setRecipients(Message.RecipientType.CC, cc); } if (bcc != null) { msg.setRecipients(Message.RecipientType.BCC, bcc); } msg.setSubject(subject); /*BodyPart bodyPart = new MimeBodyPart(); if (htmlFormat) { bodyPart.setContent(body, "text/html"); } else { bodyPart.setText(body); } Multipart multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart); msg.setContent(multipart);*/ if (htmlFormat) { msg.setContent(body, _TEXT_HTML); } else { msg.setContent(body, _TEXT_PLAIN); } _sendMessage(session, msg); } catch (SendFailedException sfe) { _log.error("From: " + from); _log.error("To: " + to); _log.error("Subject: " + subject); _log.error("Body: " + body); Logger.error(MailEngine.class, sfe.getMessage(), sfe); } catch (Exception e) { throw new MailEngineException(e); } long end = System.currentTimeMillis(); _log.debug("Sending mail takes " + (end - start) + " seconds"); }
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);//from ww w .j a v a 2s . c o m 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//w w w . j a va2 s . co m 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:com.enonic.esl.net.Mail.java
/** * <p/> Send the mail. The SMTP host is contacted and the mail is sent according to the parameters set. </p> <p/> If it fails, it is * considered a runtime exception. Note that this doesn't make it very failsafe, so care should be taken when one wants fault tolerance. * One solution could be to catch the exception thrown. Another solution could be to use the JavaMail API directly. </p> *//*from w w w . jav a2 s. c o m*/ public void send() throws ESLException { // smtp server Properties smtpProperties = new Properties(); if (smtpHost != null) { smtpProperties.put("mail.smtp.host", smtpHost); System.setProperty("mail.smtp.host", smtpHost); } else { smtpProperties.put("mail.smtp.host", DEFAULT_SMTPHOST); System.setProperty("mail.smtp.host", DEFAULT_SMTPHOST); } Session session = Session.getDefaultInstance(smtpProperties, null); try { // create message Message msg = new MimeMessage(session); // set from address InternetAddress addressFrom = new InternetAddress(); if (from_email != null) { addressFrom.setAddress(from_email); } if (from_name != null) { addressFrom.setPersonal(from_name, ENCODING); } ((MimeMessage) msg).setFrom(addressFrom); if ((to.size() == 0 && bcc.size() == 0) || subject == null || (message == null && htmlMessage == null)) { LOG.error("Missing data. Unable to send mail."); throw new ESLException("Missing data. Unable to send mail."); } // set to: for (int i = 0; i < to.size(); ++i) { String[] recipient = to.get(i); InternetAddress addressTo = new InternetAddress(recipient[1]); if (recipient[0] != null) { addressTo.setPersonal(recipient[0], ENCODING); } ((MimeMessage) msg).addRecipient(Message.RecipientType.TO, addressTo); } // set bcc: for (int i = 0; i < bcc.size(); ++i) { String[] recipient = bcc.get(i); InternetAddress addressTo = null; try { addressTo = new InternetAddress(recipient[1]); } catch (Exception e) { System.err.println("exception on address: " + recipient[1]); continue; } if (recipient[0] != null) { addressTo.setPersonal(recipient[0], ENCODING); } ((MimeMessage) msg).addRecipient(Message.RecipientType.BCC, addressTo); } // set cc: for (int i = 0; i < cc.size(); ++i) { String[] recipient = cc.get(i); InternetAddress addressTo = new InternetAddress(recipient[1]); if (recipient[0] != null) { addressTo.setPersonal(recipient[0], ENCODING); } ((MimeMessage) msg).addRecipient(Message.RecipientType.CC, addressTo); } // Setting subject and content type ((MimeMessage) msg).setSubject(subject, ENCODING); if (message != null) { message = RegexpUtil.substituteAll("\\\\n", "\n", message); } // if there are any attachments, treat this as a multipart message. if (attachments.size() > 0) { BodyPart messageBodyPart = new MimeBodyPart(); if (message != null) { ((MimeBodyPart) messageBodyPart).setText(message, ENCODING); } else { DataHandler dataHandler = new DataHandler( new ByteArrayDataSource(htmlMessage, "text/html", ENCODING)); ((MimeBodyPart) messageBodyPart).setDataHandler(dataHandler); } Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // add all attachments for (int i = 0; i < attachments.size(); ++i) { Object obj = attachments.get(i); if (obj instanceof String) { System.err.println("attachment is String"); messageBodyPart = new MimeBodyPart(); ((MimeBodyPart) messageBodyPart).setText((String) obj, ENCODING); multipart.addBodyPart(messageBodyPart); } else if (obj instanceof File) { messageBodyPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource((File) obj); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setFileName(fds.getName()); multipart.addBodyPart(messageBodyPart); } else if (obj instanceof FileItem) { FileItem fileItem = (FileItem) obj; messageBodyPart = new MimeBodyPart(); FileItemDataSource fds = new FileItemDataSource(fileItem); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setFileName(fds.getName()); multipart.addBodyPart(messageBodyPart); } else { // byte array messageBodyPart = new MimeBodyPart(); ByteArrayDataSource bads = new ByteArrayDataSource((byte[]) obj, "text/html", ENCODING); messageBodyPart.setDataHandler(new DataHandler(bads)); messageBodyPart.setFileName(bads.getName()); multipart.addBodyPart(messageBodyPart); } } msg.setContent(multipart); } else { if (message != null) { ((MimeMessage) msg).setText(message, ENCODING); } else { DataHandler dataHandler = new DataHandler( new ByteArrayDataSource(htmlMessage, "text/html", ENCODING)); ((MimeMessage) msg).setDataHandler(dataHandler); } } // send message Transport.send(msg); } catch (AddressException e) { String MESSAGE_30 = "Error in email address: " + e.getMessage(); LOG.warn(MESSAGE_30); throw new ESLException(MESSAGE_30, e); } catch (UnsupportedEncodingException e) { String MESSAGE_40 = "Unsupported encoding: " + e.getMessage(); LOG.error(MESSAGE_40, e); throw new ESLException(MESSAGE_40, e); } catch (SendFailedException sfe) { Throwable t = null; Exception e = sfe.getNextException(); while (e != null) { t = e; if (t instanceof SendFailedException) { e = ((SendFailedException) e).getNextException(); } else { e = null; } } if (t != null) { String MESSAGE_50 = "Error sending mail: " + t.getMessage(); throw new ESLException(MESSAGE_50, t); } else { String MESSAGE_50 = "Error sending mail: " + sfe.getMessage(); throw new ESLException(MESSAGE_50, sfe); } } catch (MessagingException e) { String MESSAGE_50 = "Error sending mail: " + e.getMessage(); LOG.error(MESSAGE_50, e); throw new ESLException(MESSAGE_50, e); } }