View Javadoc

1   package org.paneris.jammyjoes.mail;
2   
3   import javax.mail.Message;
4   import javax.mail.MessagingException;
5   import javax.mail.Session;
6   import javax.mail.internet.AddressException;
7   import javax.mail.internet.InternetAddress;
8   import javax.mail.internet.MimeBodyPart;
9   import javax.mail.internet.MimeMessage;
10  import javax.mail.internet.MimeMultipart;
11  
12  public class MimeMail {
13  
14    private MimeBodyPart htmlPart;
15    private MimeMessage message;
16    private MimeMultipart parts;
17  
18    public MimeMail(Session session) throws AddressException, MessagingException {
19      message = new MimeMessage(session);
20      parts = new MimeMultipart("related");
21      htmlPart = new MimeBodyPart();
22      addBodyPart(htmlPart);
23      message.setContent(parts);
24     }
25  
26    public void setContentText(String string) throws MessagingException {
27      htmlPart.setContent(string, "text/html");
28    }
29  
30    public void setRecipient(String to) throws MessagingException {
31      message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
32    }
33  
34    public void addRecipient(String to) throws MessagingException {
35      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
36    }
37  
38    public void setFrom(String from) throws MessagingException {
39      message.setFrom(new InternetAddress(from));
40    }
41  
42    public void setSubject(String subject) throws MessagingException {
43      message.setSubject(subject);
44    }
45  
46    public String getRecipient() throws MessagingException {
47      return message.getAllRecipients()[0].toString();
48    }
49  
50    public String getSubject() throws MessagingException {
51      return message.getSubject();
52    }
53  
54    public MimeMultipart getContent() {
55      return parts;
56    }
57  
58    public Message getMessage() {
59      return message;
60    }
61    
62    protected void addBodyPart(MimeBodyPart part) throws MessagingException {
63      parts.addBodyPart(part);
64    }
65   
66  }