List of usage examples for javax.mail MessagingException getMessage
public String getMessage()
From source file:org.sonarqube.tests.issue.IssueNotificationsTest.java
private List<String> extractRecipients(List<WiserMessage> messages) { return messages.stream().map(m -> { try {//from w ww .j a v a2 s. c om return m.getMimeMessage().getHeader("To", null); } catch (MessagingException e) { fail(e.getMessage(), e); return null; } }).collect(Collectors.toList()); }
From source file:org.apache.hupa.server.handler.AbstractFetchMessagesHandler.java
@Override protected FetchMessagesResult executeInternal(A action, ExecutionContext context) throws ActionException { User user = getUser();//from w w w . jav a2 s .c o m IMAPFolder folder = action.getFolder(); if (folder == null) { folder = new IMAPFolder(user.getSettings().getInboxFolderName()); } com.sun.mail.imap.IMAPFolder f = null; int start = action.getStart(); int offset = action.getOffset(); try { IMAPStore store = cache.get(user); f = (com.sun.mail.imap.IMAPFolder) store.getFolder(folder.getFullName()); // check if the folder is open, if not open it read only if (f.isOpen() == false) { f.open(com.sun.mail.imap.IMAPFolder.READ_ONLY); } // if the folder is empty we have no need to process int exists = f.getMessageCount(); if (exists == 0) { return new FetchMessagesResult(new ArrayList<org.apache.hupa.shared.data.Message>(), start, offset, 0, 0); } MessageConvertArray convArray = getMessagesToConvert(f, action); return new FetchMessagesResult(convert(offset, f, convArray.getMesssages()), start, offset, convArray.getRealCount(), f.getUnreadMessageCount()); } catch (MessagingException e) { logger.info("Error fetching messages in folder: " + folder.getFullName() + " " + e.getMessage()); // Folder can not contain messages return new FetchMessagesResult(new ArrayList<org.apache.hupa.shared.data.Message>(), start, offset, 0, 0); } catch (Exception e) { e.printStackTrace(); logger.error("Error while fetching headers for user " + user.getName() + " in folder " + folder, e); throw new ActionException( "Error while fetching headers for user " + user.getName() + " in folder " + folder); } finally { if (f != null && f.isOpen()) { try { f.close(false); } catch (MessagingException e) { // we don't care to much about an exception on close here... } } } }
From source file:business.services.MailService.java
public void notifyLab(@NotNull LabRequestRepresentation labRequest) { log.info("Notify lab for lab request " + labRequest.getId() + "."); Lab lab = labRequest.getLab();//from w w w. j ava2 s . c o m if (lab.getEmailAddresses() == null || lab.getEmailAddresses().isEmpty()) { log.warn("No email address set for lab " + lab.getNumber()); return; } String recipients = String.join(", ", lab.getEmailAddresses()); log.info("Sending notification to " + recipients); try { MimeMessageHelper message = new MimeMessageHelper(mailSender.createMimeMessage()); for (String email : lab.getEmailAddresses()) { message.addTo(email); } message.setFrom(getFrom(), fromName); message.setReplyTo(replyAddress, replyName); message.setSubject(String.format("PALGA-verzoek aan laboratorium, aanvraagnummer: %s", labRequest.getLabRequestCode())); String labRequestLink = getLink("/#/lab-request/view/" + labRequest.getId()); String body = String.format(labNotificationTemplate, labRequestLink, // %1 labRequest.getLabRequestCode(), // %2 labRequest.getRequest().getTitle(), // %3 labRequest.getRequesterName(), // %4 labRequest.getRequest().getPathologistName() == null ? "" : labRequest.getRequest().getPathologistName(), // %5 labRequest.getRequesterLab().getName() // %6 ); message.setText(body); mailSender.send(message.getMimeMessage()); } catch (MessagingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } }
From source file:eagle.common.email.EagleMailClient.java
public boolean send(String from, String to, String cc, String title, String templatePath, VelocityContext context, Map<String, File> attachments) { if (attachments == null || attachments.isEmpty()) { return send(from, to, cc, title, templatePath, context); }//ww w .j av a 2 s . co m Template t = null; List<MimeBodyPart> mimeBodyParts = new ArrayList<MimeBodyPart>(); Map<String, String> cid = new HashMap<String, String>(); for (Map.Entry<String, File> entry : attachments.entrySet()) { final String attachment = entry.getKey(); final File attachmentFile = entry.getValue(); final MimeBodyPart mimeBodyPart = new MimeBodyPart(); if (attachmentFile != null && attachmentFile.exists()) { DataSource source = new FileDataSource(attachmentFile); try { mimeBodyPart.setDataHandler(new DataHandler(source)); mimeBodyPart.setFileName(attachment); mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); mimeBodyPart.setContentID(attachment); cid.put(attachment, mimeBodyPart.getContentID()); mimeBodyParts.add(mimeBodyPart); } catch (MessagingException e) { LOG.error("Generate mail failed, got exception while attaching files: " + e.getMessage(), e); } } else { LOG.error("Attachment: " + attachment + " is null or not exists"); } } //TODO remove cid, because not used at all if (LOG.isDebugEnabled()) LOG.debug("Cid maps: " + cid); context.put("cid", cid); try { t = velocityEngine.getTemplate(BASE_PATH + templatePath); } catch (ResourceNotFoundException ex) { // LOGGER.error("Template not found:"+BASE_PATH + templatePath, ex); } if (t == null) { try { t = velocityEngine.getTemplate(templatePath); } catch (ResourceNotFoundException e) { try { t = velocityEngine.getTemplate("/" + templatePath); } catch (Exception ex) { LOG.error("Template not found:" + "/" + templatePath, ex); } } } final StringWriter writer = new StringWriter(); t.merge(context, writer); if (LOG.isDebugEnabled()) LOG.debug(writer.toString()); return this._send(from, to, cc, title, writer.toString(), mimeBodyParts); }
From source file:podd.resources.services.PublishProjectService.java
private void sendNotificationEmail() { try {//from w ww. j a va 2s .c o m User pi = project.getPrincipalInvestigator(); Set<String> ccAddresses; if (pi.equals(authenticatedUser)) { ccAddresses = Collections.emptySet(); } else { ccAddresses = Collections.singleton(authenticatedUser.getEmail()); } emailHandler.send(authenticatedUser.getEmail(), pi.getEmail(), ccAddresses, "PODD: Project Published", "The following project has been published in PODD and is now publicly available. \n\n" + " ID: " + project.getPid() + "\n" + " Title: " + project.getLocalName() + "\n" + " Description: " + project.getLabel() + "\n\n" + "You should receive an email notifying you of the projects persistant URL shortly."); } catch (MessagingException e) { final String msg = "Error sending email to " + authenticatedUser.getEmail() + ": " + e.getMessage() + " "; LOGGER.error(msg, e); errorHandler.handleError(GENERAL_MESSAGE_ID, "Email Error", msg); auditLogHelper.auditAction(ERROR, authenticatedUser, "Publish Project Service: " + msg, e.toString()); } }
From source file:org.sourceforge.net.javamail4ews.transport.EwsTransport.java
public void sendMessage(Message pMessage, Address[] addresses, Address[] ccaddresses, Address[] bccaddresses) throws MessagingException { try {//from ww w .j a v a 2 s . com EmailMessage msg = new EmailMessage(getService()); createHeaders(msg, pMessage); createAddresses(msg, pMessage, addresses, ccaddresses, bccaddresses); createSubject(msg, pMessage); createBody(msg, pMessage); sendMessage(msg); } catch (MessagingException e) { throw e; } catch (Exception e) { String message = e.getMessage(); if (message != null && message.contains( "The user account which was used to submit this request does not have the right to send mail" + " on behalf of the specified sending account")) { SMTPSendFailedException ex = new SMTPSendFailedException("send", 551, "Could not send : insufficient right to send on behalf of '" + pMessage.getFrom()[0] + "'", e, null, pMessage.getAllRecipients(), null); // ( // "Could not send : insufficient right to send on behalf of " + pMessage.getFrom()[0], e); throw ex; } else if (message != null) throw new MessagingException(message, e); else throw new MessagingException("no detailed message provided", e); } }
From source file:org.apache.axis2.transport.mail.server.SMTPWorker.java
private String processInput(String input) { if (input == null) { return Constants.COMMAND_UNKNOWN; }// w ww. j a v a 2 s .c o m if ((mail != null) && transmitionEnd) { return Constants.COMMAND_TRANSMISSION_END; } if (input.startsWith("MAIL")) { mail = new MimeMessage(Session.getInstance(new Properties(), new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return null; } })); int start = input.indexOf("<") + 1; int end; if (start <= 0) { start = input.indexOf("FROM:") + 5; end = input.length(); } else { end = input.indexOf(">"); } String from = input.substring(start, end); if ((from != null) && from.trim().length() != 0) { // TODO this is an ugly hack to get the from address in. There // should be a better way to do this. MailAddress mailFrom[] = new MailAddress[1]; mailFrom[0] = new MailAddress(from); try { mail.addFrom(mailFrom); } catch (MessagingException e) { log.info(e.getMessage()); } } return Constants.MAIL_OK; } if (input.startsWith("HELO")) { return Constants.HELO_REPLY; } else if (input.startsWith("RCPT")) { int start = input.indexOf("<") + 1; int end; if (start <= 0) { start = input.indexOf("TO:") + 3; /* * if(!input.endsWith(domain)){ System.out.println("ERROR: wrong * donmain name"); return Constants.RCPT_ERROR; } */ } else { /* * if(!input.endsWith(domain + ">")){ System.out.println("ERROR: * wrong donmain name"); return Constants.RCPT_ERROR; } */ } end = input.indexOf(">"); String toStr = input.substring(start, end); try { mail.addRecipient(Message.RecipientType.TO, new MailAddress(toStr)); receivers.add(toStr); } catch (MessagingException e) { log.info(e.getMessage()); } return Constants.RCPT_OK; } else if (input.equalsIgnoreCase("DATA")) { dataWriting = true; return Constants.DATA_START_SUCCESS; } else if (input.equalsIgnoreCase("QUIT")) { dataWriting = true; transmitionEnd = true; return Constants.COMMAND_TRANSMISSION_END; } else if (input.equals(".")) { dataWriting = false; return Constants.DATA_END_SUCCESS; } else if (input.length() == 0 && !bodyData) { bodyData = true; return null; } else if ((mail != null) && dataWriting) { try { if (bodyData) { temp += input; mail.setContent(temp, "text/xml"); //Since this is for axis2 :-) } else { mail.addHeaderLine(input); } } catch (MessagingException e) { log.info(e.getMessage()); } return null; } else { return Constants.COMMAND_UNKNOWN; } }
From source file:business.services.MailService.java
public void notifyHubuser(User hubUser, List<LabRequestRepresentation> labRequests) { if (!hubUser.isHubUser()) { log.warn("The user is no hub user: " + hubUser.getUsername()); return;// w w w . ja v a 2s .c om } List<String> codes = new ArrayList<>(); List<String> snippets = new ArrayList<>(); for (LabRequestRepresentation labRequest : labRequests) { codes.add(labRequest.getLabRequestCode()); String link = getLink("/#/lab-request/view/" + labRequest.getId()); String snippet = String.format(hubUserNotificationLabSnippet, link, // %1 labRequest.getLabRequestCode(), // %2 labRequest.getRequest().getTitle(), // %3 labRequest.getLab().getNumber(), // %4 labRequest.getLab().getName(), // %5 labRequest.getRequesterName(), // %6 labRequest.getRequest().getPathologistName() == null ? "" : labRequest.getRequest().getPathologistName(), // %7 labRequest.getRequesterLab().getNumber(), // %8 labRequest.getRequesterLab().getName() // %9 ); snippets.add(snippet); } String labRequestCodes = String.join(", ", codes); String labRequestSnippets = String.join("\n", snippets); log.info("Notify hub user " + hubUser.getUsername() + " for lab requests " + labRequestCodes + "."); if (hubUser.getContactData() == null || hubUser.getContactData().getEmail() == null || hubUser.getContactData().getEmail().trim().isEmpty()) { log.warn("No email address set for hub user " + hubUser.getUsername()); return; } log.info("Sending notification to " + hubUser.getContactData().getEmail()); try { MimeMessageHelper message = new MimeMessageHelper(mailSender.createMimeMessage()); message.setTo(hubUser.getContactData().getEmail()); message.setFrom(getFrom(), fromName); message.setReplyTo(replyAddress, replyName); message.setSubject( String.format("PALGA-verzoek aan laboratoria, aanvraagnummers: %s", labRequestCodes)); String body = String.format(hubUserNotificationTemplate, labRequestSnippets /* %1 */); message.setText(body); mailSender.send(message.getMimeMessage()); } catch (MessagingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } }
From source file:org.bibsonomy.util.MailUtils.java
/** * Method to send the password reminder mail * //ww w. j a v a 2 s .com * @param userName * @param userEmail * @param inetAddress TODO: unused!!! * @param locale * @param maxmin * @param tmppw * @return true, if the mail could be send without errors */ public boolean sendPasswordReminderMail(final String userName, final String userEmail, final String inetAddress, final Locale locale, final int maxmin, final String tmppw) { final Object[] messagesParameters = new Object[] { userName, projectName, projectHome, projectBlog, projectEmail, maxmin, tmppw }; final String messageBody = messageSource.getMessage("reminder.mail.body", messagesParameters, locale); final String messageSubject = messageSource.getMessage("reminder.mail.subject", messagesParameters, locale); /* * set the recipients */ final String[] recipient = { userEmail }; try { sendMail(recipient, messageSubject, messageBody, projectRegistrationFromAddress); return true; } catch (final MessagingException e) { log.fatal("Could not send reminder mail: " + e.getMessage()); } return false; }
From source file:org.jahia.modules.gateway.decoders.BaseMailDecoder.java
protected JahiaUser getSender(Message message) { JahiaUser user = null;//w w w . ja v a 2 s . c om try { Address[] senders = message.getFrom(); String from = null; if (senders != null && senders.length > 0) { from = ((InternetAddress) senders[0]).getAddress(); } if (StringUtils.isNotBlank(from)) { Properties userProperties = new Properties(); userProperties.setProperty("j:email", from); Set<JCRUserNode> users = getUserManagerService().searchUsers(userProperties); user = users != null && !users.isEmpty() ? users.iterator().next().getJahiaUser() : null; } } catch (MessagingException e) { logger.warn("Unable to retrieve Jahia user that corresponds" + " to the e-mail sender. Cause: " + e.getMessage(), e); } return user; }