KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mail > SendMail


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.mail;
4
5 import java.util.Date JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import javax.mail.Authenticator JavaDoc;
11 import javax.mail.Message JavaDoc;
12 import javax.mail.MessagingException JavaDoc;
13 import javax.mail.Multipart JavaDoc;
14 import javax.mail.Session JavaDoc;
15 import javax.mail.Transport JavaDoc;
16 import javax.mail.internet.InternetAddress JavaDoc;
17 import javax.mail.internet.MimeBodyPart JavaDoc;
18 import javax.mail.internet.MimeMessage JavaDoc;
19 import javax.mail.internet.MimeMultipart JavaDoc;
20
21 /**
22  * Sends e-mails in an easy way.
23  *
24  * todo i18n
25  */

26 public class SendMail {
27
28     private static SmtpServer defaultSmtpServer = null;
29     /**
30      * Sets default SMTP server.
31      *
32      * @param server default SMTP server
33      */

34     public static void setDefaultSmtpServer(SmtpServer server) {
35         defaultSmtpServer = server;
36     }
37     /**
38      * Returns default SMTP server.
39      *
40      * @return default SMTP server
41      */

42     public static SmtpServer getDefaultSmtpServer() {
43         return defaultSmtpServer;
44     }
45
46
47     // ---------------------------------------------------------------- send
48

49     /**
50      * Sends an email through default SMTP server.
51      *
52      * @param email email
53      *
54      * @exception MessagingException
55      */

56     public static void send(Email email) throws MessagingException JavaDoc {
57         send(email, null);
58     }
59
60     /**
61      * Sends an email through SMTP server.
62      *
63      * @param email email
64      * @param smtpServer SMTP server or null for default one
65      *
66      * @exception MessagingException
67      */

68     public static void send(Email email, SmtpServer smtpServer) throws MessagingException JavaDoc {
69         if (smtpServer == null) {
70             smtpServer = defaultSmtpServer;
71         }
72
73         Properties JavaDoc props = new Properties JavaDoc();
74         props.put("mail.smtp.host", smtpServer.getHost());
75         Authenticator JavaDoc auth = null;
76         if (smtpServer.getUsername() != null) {
77             props.put("mail.smtp.auth", "true");
78             auth = new SmtpSimpleAuthenticator(smtpServer.getUsername(), smtpServer.getPassword());
79         }
80         Session JavaDoc session = Session.getInstance(props, auth);
81
82         Message JavaDoc msg = new MimeMessage JavaDoc(session);
83         msg.setFrom(new InternetAddress JavaDoc(email.getFrom()));
84
85         int totalTo = email.getTo().length;
86         InternetAddress JavaDoc[] address = new InternetAddress JavaDoc[totalTo];
87         for (int i = 0; i < totalTo; i++) {
88             address[i] = new InternetAddress JavaDoc(email.getTo()[i]);
89         }
90         msg.setRecipients(Message.RecipientType.TO, address);
91
92         if (email.getCc() != null) {
93             int totalCc = email.getCc().length;
94             address = new InternetAddress JavaDoc[totalCc];
95             for (int i = 0; i < totalCc; i++) {
96                 address[i] = new InternetAddress JavaDoc(email.getCc()[i]);
97             }
98             msg.setRecipients(Message.RecipientType.CC, address);
99         }
100         if (email.getBcc() != null) {
101             int totalBcc = email.getBcc().length;
102             address = new InternetAddress JavaDoc[totalBcc];
103             for (int i = 0; i < totalBcc; i++) {
104                 address[i] = new InternetAddress JavaDoc(email.getBcc()[i]);
105             }
106             msg.setRecipients(Message.RecipientType.BCC, address);
107         }
108
109         msg.setSubject(email.getSubject());
110         Date JavaDoc date = email.getSentDate();
111         if (date == null) {
112             date = new Date JavaDoc();
113         }
114         msg.setSentDate(date);
115         HashMap JavaDoc headers = email.getHeaders();
116         Iterator JavaDoc it = headers.keySet().iterator();
117         while (it.hasNext()) {
118             String JavaDoc name = (String JavaDoc) it.next();
119             String JavaDoc value = (String JavaDoc) headers.get(name);
120             msg.setHeader(name, value);
121         }
122         
123
124         MimeBodyPart JavaDoc messageData = new MimeBodyPart JavaDoc();
125
126         if (email.isHtmlMessage()) {
127             messageData.setContent(email.getMessage(), "text/html");
128         } else {
129             //messageBodyPart.setContent(email.getMessage(), "text/plain; charset=\"Windows-1250\";");
130
//messageBodyPart.setText(email.getMessage(), "Windows-1250");
131
messageData.setText(email.getMessage());
132         }
133
134         Multipart JavaDoc multipart = new MimeMultipart JavaDoc();
135         multipart.addBodyPart(messageData);
136
137         int totalAttachments = email.getTotalAttachments();
138         for (int i = 0; i < totalAttachments; i++) {
139             multipart.addBodyPart(email.getAttachmentBodyPart(i));
140         }
141     
142         msg.setContent(multipart);
143         Transport.send(msg);
144     }
145
146 }
147
Popular Tags