001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.mail;
004:
005: import java.util.Date;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Properties;
009:
010: import javax.mail.Authenticator;
011: import javax.mail.Message;
012: import javax.mail.MessagingException;
013: import javax.mail.Multipart;
014: import javax.mail.Session;
015: import javax.mail.Transport;
016: import javax.mail.internet.InternetAddress;
017: import javax.mail.internet.MimeBodyPart;
018: import javax.mail.internet.MimeMessage;
019: import javax.mail.internet.MimeMultipart;
020:
021: /**
022: * Sends e-mails in an easy way.
023: *
024: * todo i18n
025: */
026: public class SendMail {
027:
028: private static SmtpServer defaultSmtpServer;
029:
030: /**
031: * Sets default SMTP server.
032: *
033: * @param server default SMTP server
034: */
035: public static void setDefaultSmtpServer(SmtpServer server) {
036: defaultSmtpServer = server;
037: }
038:
039: /**
040: * Returns default SMTP server.
041: *
042: * @return default SMTP server
043: */
044: public static SmtpServer getDefaultSmtpServer() {
045: return defaultSmtpServer;
046: }
047:
048: // ---------------------------------------------------------------- send
049:
050: /**
051: * Sends an email through default SMTP server.
052: *
053: * @param email email
054: *
055: * @exception MessagingException
056: */
057: public static void send(Email email) throws MessagingException {
058: send(email, null);
059: }
060:
061: /**
062: * Sends an email through SMTP server.
063: *
064: * @param email email
065: * @param smtpServer SMTP server or null for default one
066: *
067: * @exception MessagingException
068: */
069: public static void send(Email email, SmtpServer smtpServer)
070: throws MessagingException {
071: if (smtpServer == null) {
072: smtpServer = defaultSmtpServer;
073: }
074:
075: Properties props = new Properties();
076: props.put("mail.smtp.host", smtpServer.getHost());
077: Authenticator auth = null;
078: if (smtpServer.getUsername() != null) {
079: props.put("mail.smtp.auth", "true");
080: auth = new SmtpSimpleAuthenticator(
081: smtpServer.getUsername(), smtpServer.getPassword());
082: }
083: Session session = Session.getInstance(props, auth);
084:
085: Message msg = new MimeMessage(session);
086: msg.setFrom(new InternetAddress(email.getFrom()));
087:
088: int totalTo = email.getTo().length;
089: InternetAddress[] address = new InternetAddress[totalTo];
090: for (int i = 0; i < totalTo; i++) {
091: address[i] = new InternetAddress(email.getTo()[i]);
092: }
093: msg.setRecipients(Message.RecipientType.TO, address);
094:
095: if (email.getCc() != null) {
096: int totalCc = email.getCc().length;
097: address = new InternetAddress[totalCc];
098: for (int i = 0; i < totalCc; i++) {
099: address[i] = new InternetAddress(email.getCc()[i]);
100: }
101: msg.setRecipients(Message.RecipientType.CC, address);
102: }
103: if (email.getBcc() != null) {
104: int totalBcc = email.getBcc().length;
105: address = new InternetAddress[totalBcc];
106: for (int i = 0; i < totalBcc; i++) {
107: address[i] = new InternetAddress(email.getBcc()[i]);
108: }
109: msg.setRecipients(Message.RecipientType.BCC, address);
110: }
111:
112: msg.setSubject(email.getSubject());
113: Date date = email.getSentDate();
114: if (date == null) {
115: date = new Date();
116: }
117: msg.setSentDate(date);
118: HashMap headers = email.getHeaders();
119: Iterator it = headers.keySet().iterator();
120: while (it.hasNext()) {
121: String name = (String) it.next();
122: String value = (String) headers.get(name);
123: msg.setHeader(name, value);
124: }
125:
126: MimeBodyPart messageData = new MimeBodyPart();
127:
128: if (email.isHtmlMessage()) {
129: messageData.setContent(email.getMessage(), "text/html");
130: } else {
131: //messageBodyPart.setContent(email.getMessage(), "text/plain; charset=\"Windows-1250\";");
132: //messageBodyPart.setText(email.getMessage(), "Windows-1250");
133: messageData.setText(email.getMessage());
134: }
135:
136: Multipart multipart = new MimeMultipart();
137: multipart.addBodyPart(messageData);
138:
139: int totalAttachments = email.getTotalAttachments();
140: for (int i = 0; i < totalAttachments; i++) {
141: multipart.addBodyPart(email.getAttachmentBodyPart(i));
142: }
143:
144: msg.setContent(multipart);
145: Transport.send(msg);
146: }
147:
148: }
|