List of usage examples for javax.mail MessagingException getMessage
public String getMessage()
From source file:org.alfresco.email.server.impl.subetha.SubethaEmailMessage.java
public SubethaEmailMessage(InputStream dataInputStream) { MimeMessage mimeMessage = null;/*from ww w . j a v a2s . c o m*/ try { mimeMessage = new MimeMessage(Session.getDefaultInstance(System.getProperties()), dataInputStream); } catch (MessagingException e) { throw new EmailMessageException(ERR_FAILED_TO_CREATE_MIME_MESSAGE, e.getMessage()); } processMimeMessage(mimeMessage); }
From source file:org.opencastproject.kernel.mail.SmtpService.java
/** * Callback from the OSGi <code>ConfigurationAdmin</code> on configuration changes. * /*from w ww . java 2s . c om*/ * @param properties * the configuration properties * @throws ConfigurationException * if configuration fails */ @SuppressWarnings("rawtypes") @Override public void updated(Dictionary properties) throws ConfigurationException { // Read the mail server properties mailProperties.clear(); // Mail transport protocol mailTransport = StringUtils.trimToNull((String) properties.get(OPT_MAIL_TRANSPORT)); if (!("smtp".equals(mailTransport) || "smtps".equals(mailTransport))) { if (mailTransport != null) logger.warn("'{}' procotol not supported. Reverting to default: '{}'", mailTransport, DEFAULT_MAIL_TRANSPORT); mailTransport = DEFAULT_MAIL_TRANSPORT; logger.debug("Mail transport protocol defaults to '{}'", mailTransport); } else { logger.debug("Mail transport protocol is '{}'", mailTransport); } logger.info("Mail transport protocol is '{}'", mailTransport); mailProperties.put(OPT_MAIL_TRANSPORT, mailTransport); // The mail host is mandatory String propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_HOST_SUFFIX; mailHost = StringUtils.trimToNull((String) properties.get(propName)); if (mailHost == null) throw new ConfigurationException(propName, "is not set"); logger.debug("Mail host is {}", mailHost); mailProperties.put(propName, mailHost); // Mail port propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_PORT_SUFFIX; String mailPort = StringUtils.trimToNull((String) properties.get(propName)); if (mailPort == null) { mailPort = DEFAULT_MAIL_PORT; logger.debug("Mail server port defaults to '{}'", mailPort); } else { logger.debug("Mail server port is '{}'", mailPort); } mailProperties.put(propName, mailPort); // TSL over SMTP support propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_TLS_ENABLE_SUFFIX; String smtpStartTLSStr = StringUtils.trimToNull((String) properties.get(propName)); boolean smtpStartTLS = Boolean.parseBoolean(smtpStartTLSStr); if (smtpStartTLS) { mailProperties.put(propName, "true"); logger.debug("TLS over SMTP is enabled"); } else { logger.debug("TLS over SMTP is disabled"); } // Mail user mailUser = StringUtils.trimToNull((String) properties.get(OPT_MAIL_USER)); if (mailUser != null) { mailProperties.put(OPT_MAIL_USER, mailUser); logger.debug("Mail user is '{}'", mailUser); } else { logger.debug("Sending mails to {} without authentication", mailHost); } // Mail password mailPassword = StringUtils.trimToNull((String) properties.get(OPT_MAIL_PASSWORD)); if (mailPassword != null) { mailProperties.put(OPT_MAIL_PASSWORD, mailPassword); logger.debug("Mail password set"); } // Mail sender String mailFrom = StringUtils.trimToNull((String) properties.get(OPT_MAIL_FROM)); if (mailFrom == null) { logger.debug("Mail sender defaults to {}", mailFrom); } else { logger.debug("Mail sender is '{}'", mailFrom); } mailProperties.put(OPT_MAIL_FROM, mailFrom); // Authentication propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_AUTH_SUFFIX; mailProperties.put(propName, Boolean.toString(mailUser != null)); // Mail debugging String mailDebug = StringUtils.trimToNull((String) properties.get(OPT_MAIL_DEBUG)); if (mailDebug != null) { boolean mailDebugEnabled = Boolean.parseBoolean(mailDebug); mailProperties.put(OPT_MAIL_DEBUG, Boolean.toString(mailDebugEnabled)); logger.info("Mail debugging is {}", mailDebugEnabled ? "enabled" : "disabled"); } defaultMailSession = null; logger.info("Mail service configured with {}", mailHost); Properties props = getSession().getProperties(); for (String key : props.stringPropertyNames()) logger.info("{}: {}", key, props.getProperty(key)); // Test String mailTest = StringUtils.trimToNull((String) properties.get(OPT_MAIL_TEST)); if (mailTest != null && Boolean.parseBoolean(mailTest)) { logger.info("Sending test message to {}", mailFrom); try { sendTestMessage(mailFrom); } catch (MessagingException e) { logger.error("Error sending test message to " + mailFrom + ": " + e.getMessage()); while (e.getNextException() != null) { Exception ne = e.getNextException(); logger.error("Error sending test message to " + mailFrom + ": " + ne.getMessage()); if (ne instanceof MessagingException) e = (MessagingException) ne; else break; } throw new ConfigurationException(OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_HOST_SUFFIX, "Failed to send test message to " + mailFrom); } } }
From source file:com.cubusmail.gwtui.server.services.UserAccountService.java
/** * Parse out, the uncomplete Adresses/*from w ww . ja va 2s.c o m*/ * * @param addressLine * @return * @throws MessagingException */ public String[] parsePreviousAndLastAddress(String addressLine) { String[] result = new String[] { "", "" }; try { InternetAddress[] addresses = InternetAddress.parse(addressLine, false); if (addresses != null) { if (addresses.length > 1) { InternetAddress[] dest = new InternetAddress[addresses.length - 1]; for (int i = 0; i < addresses.length - 1; i++) { dest[i] = addresses[i]; } result[0] = MessageUtils.getMailAdressString(dest, AddressStringType.COMPLETE); } result[1] = addresses[addresses.length - 1].toUnicodeString(); } } catch (MessagingException e) { // should never happen log.error(e.getMessage()); } return result; }
From source file:org.apache.james.transport.mailets.jsieve.SieveMailAdapter.java
/** * @see java.lang.Object#toString()//from w w w . j ava 2 s.c om */ public String toString() { String messageID = null; try { messageID = getMail().getMessage().getMessageID(); } catch (MessagingException e) { messageID = "<" + e.getMessage() + ">"; } return getClass().getName() + " Envelope From: " + (null == getEnvelopeFrom() ? "null" : getEnvelopeFrom()) + " Envelope To: " + (null == getEnvelopeTo() ? "null" : getEnvelopeTo()) + " Message ID: " + (null == messageID ? "null" : messageID); }
From source file:org.apache.hupa.server.handler.AbstractFetchMessagesHandler.java
protected ArrayList<org.apache.hupa.shared.data.Message> convert(int offset, com.sun.mail.imap.IMAPFolder folder, Message[] messages) throws MessagingException { ArrayList<org.apache.hupa.shared.data.Message> mList = new ArrayList<org.apache.hupa.shared.data.Message>(); // Setup fetchprofile to limit the stuff which is fetched FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE);//from w ww . j a va 2 s . c o m fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add(UIDFolder.FetchProfileItem.UID); folder.fetch(messages, fp); // loop over the fetched messages for (int i = 0; i < messages.length && i < offset; i++) { org.apache.hupa.shared.data.Message msg = new org.apache.hupa.shared.data.Message(); Message m = messages[i]; String from = null; if (m.getFrom() != null && m.getFrom().length > 0) { from = MessageUtils.decodeText(m.getFrom()[0].toString()); } msg.setFrom(from); String replyto = null; if (m.getReplyTo() != null && m.getReplyTo().length > 0) { replyto = MessageUtils.decodeText(m.getReplyTo()[0].toString()); } msg.setReplyto(replyto); ArrayList<String> to = new ArrayList<String>(); // Add to addresses Address[] toArray = m.getRecipients(RecipientType.TO); if (toArray != null) { for (Address addr : toArray) { String mailTo = MessageUtils.decodeText(addr.toString()); to.add(mailTo); } } msg.setTo(to); // Check if a subject exist and if so decode it String subject = m.getSubject(); if (subject != null) { subject = MessageUtils.decodeText(subject); } msg.setSubject(subject); // Add cc addresses Address[] ccArray = m.getRecipients(RecipientType.CC); ArrayList<String> cc = new ArrayList<String>(); if (ccArray != null) { for (Address addr : ccArray) { String mailCc = MessageUtils.decodeText(addr.toString()); cc.add(mailCc); } } msg.setCc(cc); userPreferences.addContact(from); userPreferences.addContact(to); userPreferences.addContact(replyto); userPreferences.addContact(cc); // Using sentDate since received date is not useful in the view when using fetchmail msg.setReceivedDate(m.getSentDate()); // Add flags ArrayList<IMAPFlag> iFlags = JavamailUtil.convert(m.getFlags()); ArrayList<Tag> tags = new ArrayList<Tag>(); for (String flag : m.getFlags().getUserFlags()) { if (flag.startsWith(Tag.PREFIX)) { tags.add(new Tag(flag.substring(Tag.PREFIX.length()))); } } msg.setUid(folder.getUID(m)); msg.setFlags(iFlags); msg.setTags(tags); try { msg.setHasAttachments(hasAttachment(m)); } catch (MessagingException e) { logger.debug("Unable to identify attachments in message UID:" + msg.getUid() + " subject:" + msg.getSubject() + " cause:" + e.getMessage()); logger.info(""); } mList.add(0, msg); } return mList; }
From source file:org.apache.hupa.server.service.SendMessageBaseServiceImpl.java
protected void updateHeaders(MimeMessage message, SendMessageAction action) { if (action.getInReplyTo() != null) { try {//from ww w .j av a2 s . co m message.addHeader(SConsts.HEADER_IN_REPLY_TO, action.getInReplyTo()); } catch (MessagingException e) { logger.error("Error while setting header:" + e.getMessage(), e); } } if (action.getReferences() != null) { try { message.addHeader(SConsts.HEADER_REFERENCES, action.getReferences()); } catch (MessagingException e) { logger.error("Error while setting header:" + e.getMessage(), e); } } }
From source file:org.jahia.services.workflow.jbpm.JBPMMailProducer.java
public Collection<Message> produce(final Execution execution) { final Map<String, Object> vars = ((ExecutionImpl) execution).getVariables(); Locale locale = (Locale) vars.get("locale"); if (templateKey != null) { MailTemplate template = null;/*ww w . jav a 2 s.c o m*/ MailTemplateRegistry templateRegistry = ((ProcessEngine) SpringContextSingleton .getBean("processEngine")).get(MailTemplateRegistry.class); if (locale != null) { template = (templateRegistry.getTemplate(templateKey + "." + locale.toString())); if (template == null) { template = (templateRegistry.getTemplate(templateKey + "." + locale.getLanguage())); } } if (template == null) { template = templateRegistry.getTemplate(templateKey); } setTemplate(template); } if (ServicesRegistry.getInstance().getMailService().isEnabled() && getTemplate() != null) { try { return JCRTemplate.getInstance().doExecuteWithSystemSession(null, "default", locale, new JCRCallback<Collection<Message>>() { public Collection<Message> doInJCR(JCRSessionWrapper session) throws RepositoryException { try { scriptEngine = ScriptEngineUtils.getInstance() .getEngineByName(getTemplate().getLanguage()); bindings = null; Message email = instantiateEmail(); fillFrom(email, execution, session); fillRecipients(email, execution, session); fillSubject(email, execution, session); fillContent(email, execution, session); Address[] addresses = email.getRecipients(Message.RecipientType.TO); if (addresses != null && addresses.length > 0) { return Collections.singleton(email); } else { return Collections.emptyList(); } } catch (MessagingException e) { logger.error(e.getMessage(), e); } catch (ScriptException e) { logger.error(e.getMessage(), e); } return Collections.emptyList(); } }); } catch (RepositoryException e) { logger.error(e.getMessage(), e); } } return Collections.emptyList(); }
From source file:org.dspace.app.webui.servlet.RequestItemServlet.java
protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { // First get the step int step = UIUtil.getIntParameter(request, "step"); try {/*from w w w . j ava 2 s. c om*/ switch (step) { case ENTER_FORM_PAGE: processForm(context, request, response); break; case ENTER_TOKEN: processToken(context, request, response); break; case APROVE_TOKEN: processLetter(context, request, response); break; case RESUME_REQUEST: processAttach(context, request, response); break; case RESUME_FREEACESS: processAdmin(context, request, response); break; default: processForm(context, request, response); } context.complete(); } catch (MessagingException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:org.alfresco.email.server.impl.subetha.SubethaEmailMessage.java
private void processMimeMessage(MimeMessage mimeMessage) { if (from == null) { Address[] addresses = null;/*from w w w .j a va 2 s. c o m*/ try { addresses = mimeMessage.getFrom(); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_FROM_ADDRESS, e.getMessage()); } if (addresses == null || addresses.length == 0) { //throw new EmailMessageException(ERR_NO_FROM_ADDRESS); } else { if (addresses[0] instanceof InternetAddress) { from = ((InternetAddress) addresses[0]).getAddress(); } else { from = addresses[0].toString(); } } } if (to == null) { Address[] addresses = null; try { addresses = mimeMessage.getAllRecipients(); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_TO_ADDRESS, e.getMessage()); } if (addresses == null || addresses.length == 0) { //throw new EmailMessageException(ERR_NO_TO_ADDRESS); } else { if (addresses[0] instanceof InternetAddress) { to = ((InternetAddress) addresses[0]).getAddress(); } else { to = addresses[0].toString(); } } } if (cc == null) { try { ArrayList<String> list = new ArrayList<String>(); Address[] cca = mimeMessage.getRecipients(RecipientType.CC); if (cca != null) { for (Address a : cca) { list.add(a.toString()); } } cc = list; } catch (MessagingException e) { // Do nothing - this is not a show-stopper. cc = null; } } try { subject = mimeMessage.getSubject(); //subject = encodeSubject(mimeMessage.getSubject()); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_SUBJECT, e.getMessage()); } //if (subject == null) //{ // subject = ""; // Just anti-null stub :) //} try { sentDate = mimeMessage.getSentDate(); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_SENT_DATE, e.getMessage()); } if (sentDate == null) { sentDate = new Date(); // Just anti-null stub :) } parseMessagePart(mimeMessage); attachments = new EmailMessagePart[attachmentList.size()]; attachmentList.toArray(attachments); attachmentList = null; }
From source file:hudson.tasks.MailSender.java
public boolean execute(AbstractBuild<?, ?> build, BuildListener listener) throws InterruptedException { try {// w w w.j a va2 s.com MimeMessage mail = getMail(build, listener); if (mail != null) { // if the previous e-mail was sent for a success, this new e-mail // is not a follow up AbstractBuild<?, ?> pb = build.getPreviousBuild(); if (pb != null && pb.getResult() == Result.SUCCESS) { mail.removeHeader("In-Reply-To"); mail.removeHeader("References"); } Address[] allRecipients = mail.getAllRecipients(); if (allRecipients != null) { StringBuilder buf = new StringBuilder("Sending e-mails to:"); for (Address a : allRecipients) buf.append(' ').append(a); listener.getLogger().println(buf); Transport.send(mail); build.addAction(new MailMessageIdAction(mail.getMessageID())); } else { listener.getLogger().println(Messages.MailSender_ListEmpty()); } } } catch (MessagingException e) { e.printStackTrace(listener.error(e.getMessage())); } catch (UnsupportedEncodingException e) { e.printStackTrace(listener.error(e.getMessage())); } finally { CHECKPOINT.report(); } return true; }