@Test public void testParseRawMessageWithFakeFrom() { plugin.parseRawMessage("Subject:Greetings!\n\nFrom:user@example.org\n\nDear John,\nHello and Goodbye!", this.mail); Assert.assertEquals("From:user@example.org\r\n\r\nDear John,\r\nHello and Goodbye!\r\n", this.mail.getTextPart()); Assert.assertEquals("Greetings!", this.mail.getSubject()); Assert.assertNull(this.mail.getFrom()); Assert.assertEquals(0, this.mail.getHeaders().size()); }
protected void parseRawMessage(String rawMessage, Mail toMail) { if (toMail == null) { throw new IllegalArgumentException("The target Mail can't be null"); } else if (rawMessage == null) { throw new IllegalArgumentException("rawMessage can't be null"); } else if (StringUtils.isBlank(rawMessage)) { throw new IllegalArgumentException("rawMessage can't be empty"); } try { BufferedReader input = new BufferedReader(new StringReader(rawMessage)); String line; StringWriter result = new StringWriter(); PrintWriter output = new PrintWriter(result); boolean headersFound = false; line = input.readLine(); Matcher m = SMTP_HEADER.matcher(line); while (line != null && m.matches()) { String header = m.group(1); String value = m.group(2); line = input.readLine(); while (line != null && (line.startsWith(" ") || line.startsWith("\t"))) { value += line; line = input.readLine(); } if (header.equals(SUBJECT)) { toMail.setSubject(value); } else if (header.equals(FROM)) { toMail.setFrom(value); } else { toMail.setHeader(header, value); } if (line != null) { m.reset(line); } headersFound = true; } if (headersFound && line != null && StringUtils.isBlank(line)) { line = input.readLine(); } else { if (headersFound) { LOGGER.warn("Mail body does not contain an empty line between the headers and the body."); } } if (line == null) { toMail.setTextPart(""); return; } do { output.print(line + "\r\n"); } while ((line = input.readLine()) != null); toMail.setTextPart(result.toString()); } catch (IOException ioe) { LOGGER.error("Unexpected IO exception while preparing a mail", ioe); } }
MailSenderPlugin extends XWikiDefaultPlugin { protected void parseRawMessage(String rawMessage, Mail toMail) { if (toMail == null) { throw new IllegalArgumentException("The target Mail can't be null"); } else if (rawMessage == null) { throw new IllegalArgumentException("rawMessage can't be null"); } else if (StringUtils.isBlank(rawMessage)) { throw new IllegalArgumentException("rawMessage can't be empty"); } try { BufferedReader input = new BufferedReader(new StringReader(rawMessage)); String line; StringWriter result = new StringWriter(); PrintWriter output = new PrintWriter(result); boolean headersFound = false; line = input.readLine(); Matcher m = SMTP_HEADER.matcher(line); while (line != null && m.matches()) { String header = m.group(1); String value = m.group(2); line = input.readLine(); while (line != null && (line.startsWith(" ") || line.startsWith("\t"))) { value += line; line = input.readLine(); } if (header.equals(SUBJECT)) { toMail.setSubject(value); } else if (header.equals(FROM)) { toMail.setFrom(value); } else { toMail.setHeader(header, value); } if (line != null) { m.reset(line); } headersFound = true; } if (headersFound && line != null && StringUtils.isBlank(line)) { line = input.readLine(); } else { if (headersFound) { LOGGER.warn("Mail body does not contain an empty line between the headers and the body."); } } if (line == null) { toMail.setTextPart(""); return; } do { output.print(line + "\r\n"); } while ((line = input.readLine()) != null); toMail.setTextPart(result.toString()); } catch (IOException ioe) { LOGGER.error("Unexpected IO exception while preparing a mail", ioe); } } }
MailSenderPlugin extends XWikiDefaultPlugin { protected void parseRawMessage(String rawMessage, Mail toMail) { if (toMail == null) { throw new IllegalArgumentException("The target Mail can't be null"); } else if (rawMessage == null) { throw new IllegalArgumentException("rawMessage can't be null"); } else if (StringUtils.isBlank(rawMessage)) { throw new IllegalArgumentException("rawMessage can't be empty"); } try { BufferedReader input = new BufferedReader(new StringReader(rawMessage)); String line; StringWriter result = new StringWriter(); PrintWriter output = new PrintWriter(result); boolean headersFound = false; line = input.readLine(); Matcher m = SMTP_HEADER.matcher(line); while (line != null && m.matches()) { String header = m.group(1); String value = m.group(2); line = input.readLine(); while (line != null && (line.startsWith(" ") || line.startsWith("\t"))) { value += line; line = input.readLine(); } if (header.equals(SUBJECT)) { toMail.setSubject(value); } else if (header.equals(FROM)) { toMail.setFrom(value); } else { toMail.setHeader(header, value); } if (line != null) { m.reset(line); } headersFound = true; } if (headersFound && line != null && StringUtils.isBlank(line)) { line = input.readLine(); } else { if (headersFound) { LOGGER.warn("Mail body does not contain an empty line between the headers and the body."); } } if (line == null) { toMail.setTextPart(""); return; } do { output.print(line + "\r\n"); } while ((line = input.readLine()) != null); toMail.setTextPart(result.toString()); } catch (IOException ioe) { LOGGER.error("Unexpected IO exception while preparing a mail", ioe); } } MailSenderPlugin(String name, String className, XWikiContext context); }
MailSenderPlugin extends XWikiDefaultPlugin { protected void parseRawMessage(String rawMessage, Mail toMail) { if (toMail == null) { throw new IllegalArgumentException("The target Mail can't be null"); } else if (rawMessage == null) { throw new IllegalArgumentException("rawMessage can't be null"); } else if (StringUtils.isBlank(rawMessage)) { throw new IllegalArgumentException("rawMessage can't be empty"); } try { BufferedReader input = new BufferedReader(new StringReader(rawMessage)); String line; StringWriter result = new StringWriter(); PrintWriter output = new PrintWriter(result); boolean headersFound = false; line = input.readLine(); Matcher m = SMTP_HEADER.matcher(line); while (line != null && m.matches()) { String header = m.group(1); String value = m.group(2); line = input.readLine(); while (line != null && (line.startsWith(" ") || line.startsWith("\t"))) { value += line; line = input.readLine(); } if (header.equals(SUBJECT)) { toMail.setSubject(value); } else if (header.equals(FROM)) { toMail.setFrom(value); } else { toMail.setHeader(header, value); } if (line != null) { m.reset(line); } headersFound = true; } if (headersFound && line != null && StringUtils.isBlank(line)) { line = input.readLine(); } else { if (headersFound) { LOGGER.warn("Mail body does not contain an empty line between the headers and the body."); } } if (line == null) { toMail.setTextPart(""); return; } do { output.print(line + "\r\n"); } while ((line = input.readLine()) != null); toMail.setTextPart(result.toString()); } catch (IOException ioe) { LOGGER.error("Unexpected IO exception while preparing a mail", ioe); } } MailSenderPlugin(String name, String className, XWikiContext context); @Override String getName(); @Override Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context); static String[] parseAddresses(String email); MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context); Multipart createMimeMultipart(Mail mail, XWikiContext context); VelocityContext prepareVelocityContext(String fromAddr, String toAddr, String ccAddr, String bccAddr,
        VelocityContext vcontext, XWikiContext context); VelocityContext prepareVelocityContext(String fromAddr, String toAddr, String ccAddr, String bccAddr,
        Map<String, Object> parameters, XWikiContext context); boolean sendMail(Mail mailItem, XWikiContext context); boolean sendMail(Mail mailItem, MailConfiguration mailConfiguration, XWikiContext context); boolean sendMails(Collection<Mail> emails, XWikiContext context); boolean sendMails(Collection<Mail> emails, MailConfiguration mailConfiguration, XWikiContext context); int sendMailFromTemplate(String templateDocFullName, String from, String to, String cc, String bcc,
        String language, VelocityContext vcontext, XWikiContext context); int sendMailFromTemplate(String templateDocFullName, String from, String to, String cc, String bcc,
        String language, Map<String, Object> parameters, XWikiContext context); }
MailSenderPlugin extends XWikiDefaultPlugin { protected void parseRawMessage(String rawMessage, Mail toMail) { if (toMail == null) { throw new IllegalArgumentException("The target Mail can't be null"); } else if (rawMessage == null) { throw new IllegalArgumentException("rawMessage can't be null"); } else if (StringUtils.isBlank(rawMessage)) { throw new IllegalArgumentException("rawMessage can't be empty"); } try { BufferedReader input = new BufferedReader(new StringReader(rawMessage)); String line; StringWriter result = new StringWriter(); PrintWriter output = new PrintWriter(result); boolean headersFound = false; line = input.readLine(); Matcher m = SMTP_HEADER.matcher(line); while (line != null && m.matches()) { String header = m.group(1); String value = m.group(2); line = input.readLine(); while (line != null && (line.startsWith(" ") || line.startsWith("\t"))) { value += line; line = input.readLine(); } if (header.equals(SUBJECT)) { toMail.setSubject(value); } else if (header.equals(FROM)) { toMail.setFrom(value); } else { toMail.setHeader(header, value); } if (line != null) { m.reset(line); } headersFound = true; } if (headersFound && line != null && StringUtils.isBlank(line)) { line = input.readLine(); } else { if (headersFound) { LOGGER.warn("Mail body does not contain an empty line between the headers and the body."); } } if (line == null) { toMail.setTextPart(""); return; } do { output.print(line + "\r\n"); } while ((line = input.readLine()) != null); toMail.setTextPart(result.toString()); } catch (IOException ioe) { LOGGER.error("Unexpected IO exception while preparing a mail", ioe); } } MailSenderPlugin(String name, String className, XWikiContext context); @Override String getName(); @Override Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context); static String[] parseAddresses(String email); MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context); Multipart createMimeMultipart(Mail mail, XWikiContext context); VelocityContext prepareVelocityContext(String fromAddr, String toAddr, String ccAddr, String bccAddr,
        VelocityContext vcontext, XWikiContext context); VelocityContext prepareVelocityContext(String fromAddr, String toAddr, String ccAddr, String bccAddr,
        Map<String, Object> parameters, XWikiContext context); boolean sendMail(Mail mailItem, XWikiContext context); boolean sendMail(Mail mailItem, MailConfiguration mailConfiguration, XWikiContext context); boolean sendMails(Collection<Mail> emails, XWikiContext context); boolean sendMails(Collection<Mail> emails, MailConfiguration mailConfiguration, XWikiContext context); int sendMailFromTemplate(String templateDocFullName, String from, String to, String cc, String bcc,
        String language, VelocityContext vcontext, XWikiContext context); int sendMailFromTemplate(String templateDocFullName, String from, String to, String cc, String bcc,
        String language, Map<String, Object> parameters, XWikiContext context); static int ERROR_TEMPLATE_EMAIL_OBJECT_NOT_FOUND; static int ERROR; static final String EMAIL_XWIKI_CLASS_NAME; static final String ID; }