| 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 | 0 | public MimeMail(Session session) throws AddressException, MessagingException { |
| 19 | 0 | message = new MimeMessage(session); |
| 20 | 0 | parts = new MimeMultipart("related"); |
| 21 | 0 | htmlPart = new MimeBodyPart(); |
| 22 | 0 | addBodyPart(htmlPart); |
| 23 | 0 | message.setContent(parts); |
| 24 | 0 | } |
| 25 | |
|
| 26 | |
public void setContentText(String string) throws MessagingException { |
| 27 | 0 | htmlPart.setContent(string, "text/html"); |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
public void setRecipient(String to) throws MessagingException { |
| 31 | 0 | message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
| 32 | 0 | } |
| 33 | |
|
| 34 | |
public void addRecipient(String to) throws MessagingException { |
| 35 | 0 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
public void setFrom(String from) throws MessagingException { |
| 39 | 0 | message.setFrom(new InternetAddress(from)); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public void setSubject(String subject) throws MessagingException { |
| 43 | 0 | message.setSubject(subject); |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
public String getRecipient() throws MessagingException { |
| 47 | 0 | return message.getAllRecipients()[0].toString(); |
| 48 | |
} |
| 49 | |
|
| 50 | |
public String getSubject() throws MessagingException { |
| 51 | 0 | return message.getSubject(); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public MimeMultipart getContent() { |
| 55 | 0 | return parts; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public Message getMessage() { |
| 59 | 0 | return message; |
| 60 | |
} |
| 61 | |
|
| 62 | |
protected void addBodyPart(MimeBodyPart part) throws MessagingException { |
| 63 | 0 | parts.addBodyPart(part); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
} |