Sending HTML-Mail with JSPWiki
------------------------------

                 Key: JSPWIKI-258
                 URL: https://issues.apache.org/jira/browse/JSPWIKI-258
             Project: JSPWiki
          Issue Type: Improvement
          Components: Core & storage
    Affects Versions: 2.6.2
            Reporter: Holger Szillat
            Priority: Minor
             Fix For: 2.6.2


Hey, while writing my own little plugin, I wanted to send HTML-mails with 
JSPWiki, but discovered, that this was not easily possible. So I "enhanced" the 
MailUtil-class with the following code:

...
    /**
     * Get a Mime-message associated with the given engine's mail-session. Use 
this
     * to create a multipart Mime-message to be sent via the Wiki.
     * 
     * @param engine the WikiEngine for the current wiki
     * @return A MimeMessage associated with the mail-session.
     * @author Holger Szillat
     */
    public static MimeMessage getMimeMessage(WikiEngine engine) {
        Session session = getMailSession(engine);
        MimeMessage msg = new MimeMessage(session);
        
        return msg;
    }

    /**
     * <p>Sends an e-mail to a specified receiver using a JavaMail Session 
supplied
     * by a JNDI mail session factory (preferred) or a locally initialized
     * session based on properties in <code>jspwiki.properties</code>.
     * See the top-level JavaDoc for this class for a description of
     * required properties and their default values.</p>
     * <p>The e-mail address used for the <code>to</code> parameter must be in
     * RFC822 format, as described in the JavaDoc for [EMAIL PROTECTED] 
javax.mail.internet.InternetAddress}
     * and more fully at
     * <a 
href="http://www.freesoft.org/CIE/RFC/822/index.htm";>http://www.freesoft.org/CIE/RFC/822/index.htm</a>.
     * In other words, e-mail addresses should look like this:</p>
     * <blockquote><code>Snoop Dog &lt;[EMAIL PROTECTED]&gt;<br/>
     * [EMAIL PROTECTED]</code></blockquote>
     * <p>Note that the first form allows a "friendly" user name to be supplied
     * in addition to the actual e-mail address.</p>
     *
     * @param engine the WikiEngine for the current wiki
     * @param to the receiver
     * @param subject the subject line of the message
     * @param content the contents of the mail message, as plain text
     * @throws AddressException
     * @throws MessagingException
     */
    public static void sendMessage(WikiEngine engine, String to, String 
subject, String content)
    throws AddressException, MessagingException {
        Session session = getMailSession(engine);

        MimeMessage msg = getMimeMessage(engine);
        msg.setText(content, "UTF-8");
        
        sendMessage(engine, to, subject, msg);
    }

    /**
     * <p>Sends an e-mail to a specified receiver using a JavaMail Session 
supplied
     * by a JNDI mail session factory (preferred) or a locally initialized
     * session based on properties in <code>jspwiki.properties</code>.
     * See the top-level JavaDoc for this class for a description of
     * required properties and their default values.</p>
     * <p>This method should be used to send a Mime-multipart message using the 
Wiki's
     * Mail-session.
     * </p> 
     *  
     * @param engine the WikiEngine for the current wiki
     * @param to the receiver
     * @param subject the subject line of the message
     * @param content the contents of the mail message
     * @throws AddressException
     * @throws MessagingException
     * 
     * @author Holger Szillat
     */
    public static void sendMessage(WikiEngine engine, String to, String 
subject, Message content)
    throws AddressException, MessagingException
    {
        Properties props = engine.getWikiProperties();
        Session session = getMailSession(engine);
        getSenderEmailAddress(session, props);

        try
        {
            // Create and address the message
            content.setFrom(new InternetAddress(fromAddress));
            content.setRecipients(Message.RecipientType.TO, 
InternetAddress.parse(to, false));
            content.setSubject(subject);
            content.setSentDate(new Date());

            // Send and log it
            Transport.send(content);
            if (log.isInfoEnabled())
            {
                log.info("Sent e-mail to=" + to + ", subject=\"" + subject + 
"\", used "
                         + (c_useJndi ? "JNDI" : "standalone") + " mail 
session.");
            }
        }
        catch (MessagingException e)
        {
            log.error(e);
            throw e;
        }
    }
...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to