@Override public int sendMail(Mail mail) { int result = 0; try { getProtectedPlugin().sendMail(mail, this.context); } catch (Exception e) { // If the exception is a null pointer exception there is no message and e.getMessage() is null. if (e.getMessage() != null) { this.context.put("error", e.getMessage()); } LOGGER.error("Failed to send email [" + mail.toString() + "]", e); result = -1; } return result; }
/** * Uses an XWiki document to build the message subject and context, based on variables stored in a map. * Sends the email. * * @param templateDocFullName Full name of the template to be used (example: XWiki.MyEmailTemplate). The template * needs to have an XWiki.Email object attached * @param from Email sender * @param to Email recipient * @param cc Email Carbon Copy * @param bcc Email Hidden Carbon Copy * @param language Language of the email * @param parameters variables to be passed to the velocity context * @return True if the email has been sent */ public int sendMailFromTemplate(String templateDocFullName, String from, String to, String cc, String bcc, String language, Map<String, Object> parameters, XWikiContext context) throws XWikiException { VelocityContext vcontext = prepareVelocityContext(from, to, cc, bcc, parameters, context); return sendMailFromTemplate(templateDocFullName, from, to, cc, bcc, language, vcontext, context); } }
/** * Filters a list of emails : removes illegal addresses * * @param email List of emails * @return An Array containing the correct adresses */ private static InternetAddress[] toInternetAddresses(String email) throws AddressException { String[] mails = parseAddresses(email); if (mails == null) { return null; } InternetAddress[] address = new InternetAddress[mails.length]; for (int i = 0; i < mails.length; i++) { address[i] = new InternetAddress(mails[i]); } return address; }
context.setURLFactory(new ExternalServletURLFactory(context)); context.setLocale(LocaleUtils.toLocale(language)); VelocityContext updatedVelocityContext = prepareVelocityContext(from, to, cc, bcc, vcontext, context); XWiki xwiki = context.getWiki(); XWikiDocument doc = xwiki.getDocument(templateDocFullName, context); String htmlContent = obj.getStringValue("html"); String subject = evaluate(subjectContent, templateDocFullName, updatedVelocityContext, context); String msg = evaluate(txtContent, templateDocFullName, updatedVelocityContext, context); String html = evaluate(htmlContent, templateDocFullName, updatedVelocityContext, context); sendMail(mail, context); return 0; } catch (Exception e) {
@Override public int sendMessageFromTemplate(String from, String to, String cc, String bcc, String language, String documentFullName, VelocityContext vcontext) { try { return getProtectedPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc, language, vcontext, this.context); } catch (Exception e) { // If the exception is a null pointer exception there is no message and e.getMessage() is null. if (e.getMessage() != null) { this.context.put("error", e.getMessage()); } LOGGER.error("sendMessageFromTemplate", e); return -1; } }
Properties props = initProperties(mailConfiguration); session = Session.getInstance(props, null); transport = session.getTransport("smtp"); MimeMessage message = createMimeMessage(mail, session, context); if (message == null) { continue;
recipients = mail.getTo() + "," + recipients; InternetAddress[] to = toInternetAddresses(recipients); recipients = mail.getHeader("Cc"); if (StringUtils.isBlank(recipients)) { recipients = mail.getCc() + "," + recipients; InternetAddress[] cc = toInternetAddresses(recipients); recipients = mail.getHeader("Bcc"); if (StringUtils.isBlank(recipients)) { recipients = mail.getBcc() + "," + recipients; InternetAddress[] bcc = toInternetAddresses(recipients); Multipart multipart = createMimeMultipart(mail, context); message.setContent(multipart); } else {
try { context.setURLFactory(new ExternalServletURLFactory(context)); VelocityContext updatedVelocityContext = prepareVelocityContext(from, to, cc, bcc, vcontext, context); XWiki xwiki = context.getWiki(); XWikiDocument doc = xwiki.getDocument(templateDocFullName, context); sendMail(mail, context); return 0; } catch (Exception e) {
/** * Send a single Mail * * @param mailItem The Mail to send * @return True if the the email has been sent */ public boolean sendMail(Mail mailItem, MailConfiguration mailConfiguration, XWikiContext context) throws MessagingException, UnsupportedEncodingException { ArrayList<Mail> mailList = new ArrayList<Mail>(); mailList.add(mailItem); return sendMails(mailList, mailConfiguration, context); }
@Override public int sendRawMessage(String from, String to, String rawMessage) { Mail email = new Mail(); email.setFrom(from); email.setTo(to); getProtectedPlugin().parseRawMessage(rawMessage, email); return sendMail(email); }
multipart.addBodyPart(createAttachmentBodyPart(attachment, context)); htmlMultipart.addBodyPart(createAttachmentBodyPart(image, context)); multipart.addBodyPart(createAttachmentBodyPart(attachment, context));
/** * {@inheritDoc} * * @see MailSender#sendMessageFromTemplate(String, String, String, String, String, String, VelocityContext) */ public int sendMessageFromTemplate(String from, String to, String cc, String bcc, String language, String documentFullName, VelocityContext vcontext) { try { return getProtectedPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc, language, vcontext, this.context); } catch (Exception e) { // If the exception is a null pointer exception there is no message and e.getMessage() is null. if (e.getMessage() != null) { this.context.put("error", e.getMessage()); } LOG.error("sendMessageFromTemplate", e); return -1; } }
Properties props = initProperties(mailConfiguration); session = Session.getInstance(props, null); transport = session.getTransport("smtp"); MimeMessage message = createMimeMessage(mail, session, context); if (message == null) { continue;
recipients = mail.getTo() + "," + recipients; InternetAddress[] to = toInternetAddresses(recipients); recipients = mail.getHeader("Cc"); if (StringUtils.isBlank(recipients)) { recipients = mail.getCc() + "," + recipients; InternetAddress[] cc = toInternetAddresses(recipients); recipients = mail.getHeader("Bcc"); if (StringUtils.isBlank(recipients)) { recipients = mail.getBcc() + "," + recipients; InternetAddress[] bcc = toInternetAddresses(recipients); Multipart multipart = createMimeMultipart(mail, context); message.setContent(multipart); } else {
/** * Send a single Mail * * @param mailItem The Mail to send * @return True if the the email has been sent */ public boolean sendMail(Mail mailItem, MailConfiguration mailConfiguration, XWikiContext context) throws MessagingException, UnsupportedEncodingException { ArrayList<Mail> mailList = new ArrayList<Mail>(); mailList.add(mailItem); return sendMails(mailList, mailConfiguration, context); }
/** * {@inheritDoc} * * @see MailSender#sendRawMessage(String, String, String) */ public int sendRawMessage(String from, String to, String rawMessage) { Mail email = new Mail(); email.setFrom(from); email.setTo(to); getProtectedPlugin().parseRawMessage(rawMessage, email); return sendMail(email); }
multipart.addBodyPart(createAttachmentBodyPart(attachment, context)); htmlMultipart.addBodyPart(createAttachmentBodyPart(image, context)); multipart.addBodyPart(createAttachmentBodyPart(attachment, context));
/** * {@inheritDoc} * * @see MailSender#sendMail(Mail) */ public int sendMail(Mail mail) { int result = 0; try { getProtectedPlugin().sendMail(mail, this.context); } catch (Exception e) { // If the exception is a null pointer exception there is no message and e.getMessage() is null. if (e.getMessage() != null) { this.context.put("error", e.getMessage()); } LOG.error("Failed to send email [" + mail.toString() + "]", e); result = -1; } return result; }
/** * Uses an XWiki document to build the message subject and context, based on variables stored in a map. Sends the * email. * * @param from Email sender * @param to Email recipient * @param cc Email Carbon Copy * @param bcc Email Hidden Carbon Copy * @param language Language of the email * @param documentFullName Full name of the template to be used (example: XWiki.MyEmailTemplate). The template needs * to have an XWiki.Email object attached * @param parameters variables to be passed to the velocity context * @return 0 on success, -1 on failure. On failure the error message is stored in the XWiki context under the * "error" key. */ public int sendMessageFromTemplate(String from, String to, String cc, String bcc, String language, String documentFullName, Map<String, Object> parameters) { try { return getProtectedPlugin().sendMailFromTemplate(documentFullName, from, to, cc, bcc, language, parameters, this.context); } catch (Exception e) { // If the exception is a null pointer exception there is no message and e.getMessage() is null. if (e.getMessage() != null) { this.context.put("error", e.getMessage()); } LOGGER.error("sendMessageFromTemplate", e); return -1; } }
/** * Send a Collection of Mails (multiple emails) * * @param emails Mail Collection * @return True in any case (TODO ?) */ public boolean sendMails(Collection<Mail> emails, XWikiContext context) throws MessagingException, UnsupportedEncodingException { // TODO: Fix the need to instantiate a new XWiki API object com.xpn.xwiki.api.XWiki xwikiApi = new com.xpn.xwiki.api.XWiki(context.getWiki(), context); return sendMails(emails, new MailConfiguration(xwikiApi), context); }