| 1 | |
package org.jtheque.core.utils; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jtheque.utils.bean.Email; |
| 20 | |
|
| 21 | |
import javax.activation.DataHandler; |
| 22 | |
import javax.activation.DataSource; |
| 23 | |
import javax.activation.FileDataSource; |
| 24 | |
import javax.mail.BodyPart; |
| 25 | |
import javax.mail.Message; |
| 26 | |
import javax.mail.MessagingException; |
| 27 | |
import javax.mail.Multipart; |
| 28 | |
import javax.mail.Session; |
| 29 | |
import javax.mail.Transport; |
| 30 | |
import javax.mail.internet.InternetAddress; |
| 31 | |
import javax.mail.internet.MimeBodyPart; |
| 32 | |
import javax.mail.internet.MimeMessage; |
| 33 | |
import javax.mail.internet.MimeMultipart; |
| 34 | |
import java.io.File; |
| 35 | |
import java.util.Date; |
| 36 | |
import java.util.Properties; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public final class MailUtils { |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
private MailUtils() { |
| 48 | 0 | super(); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public static void send(Email email, String host) throws MessagingException { |
| 59 | 0 | MimeMessage msg = createMessage(host); |
| 60 | |
|
| 61 | 0 | configureMessage(email, msg); |
| 62 | |
|
| 63 | 0 | Multipart mp = new MimeMultipart(); |
| 64 | |
|
| 65 | 0 | MimeBodyPart mbp1 = new MimeBodyPart(); |
| 66 | 0 | mbp1.setText(email.getMessage()); |
| 67 | |
|
| 68 | 0 | mp.addBodyPart(mbp1); |
| 69 | |
|
| 70 | 0 | if (!email.getAttachedFiles().isEmpty()) { |
| 71 | 0 | attachFiles(email, mp); |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | msg.setContent(mp); |
| 75 | |
|
| 76 | 0 | Transport.send(msg); |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
private static void configureMessage(Email email, MimeMessage msg) throws MessagingException { |
| 87 | 0 | msg.setFrom(new InternetAddress(email.getFrom())); |
| 88 | |
|
| 89 | 0 | for (String to : email.getTo()) { |
| 90 | 0 | msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | msg.setSubject(email.getSubject()); |
| 94 | 0 | msg.setSentDate(new Date()); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
private static MimeMessage createMessage(String host) { |
| 104 | 0 | Properties props = new Properties(); |
| 105 | 0 | props.setProperty("mail.smtp.host", host); |
| 106 | |
|
| 107 | 0 | Session session = Session.getInstance(props, null); |
| 108 | 0 | session.setDebug(false); |
| 109 | |
|
| 110 | 0 | return new MimeMessage(session); |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
private static void attachFiles(Email email, Multipart mp) throws MessagingException { |
| 121 | 0 | for (File f : email.getAttachedFiles()) { |
| 122 | 0 | BodyPart messageBodyPart = new MimeBodyPart(); |
| 123 | |
|
| 124 | 0 | DataSource source = new FileDataSource(f); |
| 125 | 0 | messageBodyPart.setDataHandler(new DataHandler(source)); |
| 126 | 0 | messageBodyPart.setFileName(source.getName()); |
| 127 | |
|
| 128 | 0 | mp.addBodyPart(messageBodyPart); |
| 129 | 0 | } |
| 130 | 0 | } |
| 131 | |
} |