Coverage report

  %line %branch
org.apache.turbine.util.mail.HtmlEmail
0% 
0% 

 1  
 package org.apache.turbine.util.mail;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.net.URL;
 20  
 
 21  
 import javax.activation.DataHandler;
 22  
 import javax.activation.URLDataSource;
 23  
 
 24  
 import javax.mail.MessagingException;
 25  
 import javax.mail.internet.MimeBodyPart;
 26  
 import javax.mail.internet.MimeMultipart;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 
 30  
 import org.apache.ecs.Document;
 31  
 import org.apache.ecs.ElementContainer;
 32  
 import org.apache.ecs.html.Body;
 33  
 import org.apache.ecs.html.Html;
 34  
 import org.apache.ecs.html.PRE;
 35  
 
 36  
 /**
 37  
  * An HTML multipart email.
 38  
  *
 39  
  * <p>This class is used to send HTML formatted email.  A text message
 40  
  * can also be set for HTML unaware email clients, such as text-based
 41  
  * email clients.
 42  
  *
 43  
  * <p>This class also inherits from MultiPartEmail, so it is easy to
 44  
  * add attachents to the email.
 45  
  *
 46  
  * <p>To send an email in HTML, one should create a HtmlEmail, then
 47  
  * use the setFrom, addTo, etc. methods.  The HTML content can be set
 48  
  * with the setHtmlMsg method.  The alternate text content can be set
 49  
  * with setTextMsg.
 50  
  *
 51  
  * <p>Either the text or HTML can be omitted, in which case the "main"
 52  
  * part of the multipart becomes whichever is supplied rather than a
 53  
  * multipart/alternative.
 54  
  *
 55  
  * @author <a href="mailto:unknown">Regis Koenig</a>
 56  
  * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
 57  
  * @version $Id: HtmlEmail.java 264148 2005-08-29 14:21:04Z henning $
 58  
  * @deprecated Use org.apache.commons.mail.HtmlEmail instead.
 59  
  */
 60  
 public class HtmlEmail extends MultiPartEmail
 61  
 {
 62  
     protected MimeMultipart htmlContent;
 63  
 
 64  
     protected String text;
 65  
     protected String html;
 66  
 
 67  
     /**
 68  
      * Basic constructor.
 69  
      *
 70  
      * @exception MessagingException.
 71  
      */
 72  
     public HtmlEmail()
 73  
             throws MessagingException
 74  0
     {
 75  0
         this.init();
 76  0
     }
 77  
 
 78  
     /**
 79  
      * Instantiates a new MimeMultipart object if it isn't already
 80  
      * instantiated.
 81  
      *
 82  
      * @return A MimeMultipart object
 83  
      */
 84  
     public MimeMultipart getHtmlContent()
 85  
     {
 86  0
         if (htmlContent == null)
 87  
         {
 88  0
             htmlContent = new MimeMultipart();
 89  
         }
 90  0
         return htmlContent;
 91  
     }
 92  
 
 93  
     /**
 94  
      * Set the text content.
 95  
      *
 96  
      * @param text A String.
 97  
      * @return An HtmlEmail.
 98  
      * @exception MessagingException.
 99  
      */
 100  
     public HtmlEmail setTextMsg(String text)
 101  
             throws MessagingException
 102  
     {
 103  0
         this.text = text;
 104  0
         return this;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Set the HTML content.
 109  
      *
 110  
      * @param html A String.
 111  
      * @return An HtmlEmail.
 112  
      * @exception MessagingException.
 113  
      */
 114  
     public HtmlEmail setHtmlMsg(String html)
 115  
             throws MessagingException
 116  
     {
 117  0
         this.html = html;
 118  0
         return this;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Set the HTML content based on an ECS document.
 123  
      *
 124  
      * @param doc A Document.
 125  
      * @return An HtmlEmail.
 126  
      * @exception MessagingException.
 127  
      */
 128  
     public HtmlEmail setHtmlMsg(Document doc)
 129  
             throws MessagingException
 130  
     {
 131  0
         return setHtmlMsg(doc.toString());
 132  
     }
 133  
 
 134  
     /**
 135  
      * Set the message.
 136  
      *
 137  
      * <p>This method overrides the MultiPartEmail setMsg() method in
 138  
      * order to send an HTML message instead of a full text message in
 139  
      * the mail body. The message is formatted in HTML for the HTML
 140  
      * part of the message, it is let as is in the alternate text
 141  
      * part.
 142  
      *
 143  
      * @param msg A String.
 144  
      * @return An Email.
 145  
      * @exception MessagingException.
 146  
      */
 147  
     public Email setMsg(String msg)
 148  
             throws MessagingException
 149  
     {
 150  0
         setTextMsg(msg);
 151  0
         setHtmlMsg(new ElementContainer(class="keyword">new Html(class="keyword">new Body()
 152  
                 .addElement(new PRE(msg)))).toString());
 153  0
         return this;
 154  
     }
 155  
 
 156  
     /**
 157  
      * Embeds an URL in the HTML.
 158  
      *
 159  
      * <p>This method allows to embed a file located by an URL into
 160  
      * the mail body.  It allows, for instance, to add inline images
 161  
      * to the email.  Inline files may be referenced with a
 162  
      * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID
 163  
      * returned by the embed function.
 164  
      *
 165  
      * <p>Example of use:<br><code><pre>
 166  
      * HtmlEmail he = new HtmlEmail();
 167  
      * he.setHtmlMsg("&lt;html&gt;&lt;img src=cid:"+embed("file:/my/image.gif","image.gif")+"&gt;&lt;/html&gt;");
 168  
      * // code to set the others email fields (not shown)
 169  
      * </pre></code>
 170  
      *
 171  
      * @param url The URL of the file.
 172  
      * @param name The name that will be set in the filename header
 173  
      * field.
 174  
      * @return A String with the Content-ID of the file.
 175  
      * @exception MessagingException.
 176  
      */
 177  
     public String embed(URL url, String name)
 178  
             throws MessagingException
 179  
     {
 180  0
         MimeBodyPart mbp = new MimeBodyPart();
 181  
 
 182  0
         mbp.setDataHandler(new DataHandler(class="keyword">new URLDataSource(url)));
 183  0
         mbp.setFileName(name);
 184  0
         mbp.setDisposition("inline");
 185  0
         String cid = org.apache.turbine.util.GenerateUniqueId.getIdentifier();
 186  0
         mbp.addHeader("Content-ID", cid);
 187  
 
 188  0
         getHtmlContent().addBodyPart(mbp);
 189  0
         return mbp.getContentID();
 190  
     }
 191  
 
 192  
     /**
 193  
      * Does the work of actually sending the email.
 194  
      *
 195  
      * @exception MessagingException, if there was an error.
 196  
      */
 197  
     public void send()
 198  
             throws MessagingException
 199  
     {
 200  0
         MimeBodyPart msgText = null;
 201  0
         MimeBodyPart msgHtml = null;
 202  
 
 203  0
         if (StringUtils.isNotEmpty(text) && StringUtils.isNotEmpty(html))
 204  
         {
 205  
             // The message in text and HTML form.
 206  0
             MimeMultipart msg = getHtmlContent();
 207  0
             msg.setSubType("alternative");
 208  0
             main.setContent(msg);
 209  
 
 210  0
             msgText = new MimeBodyPart();
 211  0
             msgHtml = new MimeBodyPart();
 212  0
             msg.addBodyPart(msgText);
 213  0
             msg.addBodyPart(msgHtml);
 214  
 
 215  
         }
 216  0
         else if (StringUtils.isNotEmpty(text))
 217  
         {
 218  
             // just text so the text part is the main part
 219  0
             msgText = main;
 220  
         }
 221  0
         else if (StringUtils.isNotEmpty(html))
 222  
         {
 223  
             // just HTML so the html part is the main part
 224  0
             msgHtml = main;
 225  
         }
 226  
         else
 227  
         {
 228  0
             msgText = main;
 229  0
             text = "NO BODY";
 230  
         }
 231  
 
 232  0
         if (msgText != null)
 233  
         {
 234  
             // add the text
 235  0
             if (charset != null)
 236  
             {
 237  0
                 msgText.setText(text, charset);
 238  
             }
 239  
             else
 240  
             {
 241  0
                 msgText.setText(text);
 242  
             }
 243  
         }
 244  
 
 245  0
         if (msgHtml != null)
 246  
         {
 247  
             // add the html
 248  0
             if (charset != null)
 249  
             {
 250  0
                 msgHtml.setContent(html, TEXT_HTML + ";charset=" + charset);
 251  
             }
 252  
             else
 253  
             {
 254  0
                 msgHtml.setContent(html, TEXT_HTML);
 255  
             }
 256  
         }
 257  
 
 258  0
         super.send();
 259  0
     }
 260  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.