Rev 3 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3 | Rev 9 | ||
---|---|---|---|
1 | package de.viathinksoft.utils.mail.sender; |
1 | package de.viathinksoft.utils.mail.sender; |
2 | 2 | ||
3 | // Ref: |
3 | // Ref: |
4 | // http://openbook.galileocomputing.de/javainsel8/javainsel_18_012.htm#mjb306e4632c440d0524d00f224d4fa1bb - Kapitel 18.12.6 |
4 | // http://openbook.galileocomputing.de/javainsel8/javainsel_18_012.htm#mjb306e4632c440d0524d00f224d4fa1bb - Kapitel 18.12.6 |
5 | // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html |
5 | // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html |
6 | 6 | ||
7 | import java.util.Properties; |
7 | import java.util.Properties; |
8 | 8 | ||
9 | import javax.mail.Authenticator; |
9 | import javax.mail.Authenticator; |
10 | import javax.mail.Message; |
10 | import javax.mail.Message; |
11 | import javax.mail.MessagingException; |
11 | import javax.mail.MessagingException; |
12 | import javax.mail.Session; |
12 | import javax.mail.Session; |
13 | import javax.mail.Transport; |
13 | import javax.mail.Transport; |
14 | import javax.mail.internet.InternetAddress; |
14 | import javax.mail.internet.InternetAddress; |
15 | import javax.mail.internet.MimeMessage; |
15 | import javax.mail.internet.MimeMessage; |
16 | 16 | ||
17 | import de.viathinksoft.utils.mail.InvalidMailAddressException; |
- | |
18 | - | ||
19 | abstract public class RawMailSender { |
17 | abstract public class RawMailSender { |
20 | 18 | ||
21 | private static final String TRANSPORT_PROTOCOL = "smtp"; |
19 | private static final String TRANSPORT_PROTOCOL = "smtp"; |
22 | 20 | ||
23 | protected Session session; |
21 | protected Session session; |
24 | protected Message msg; |
22 | protected Message msg; |
25 | protected Properties props; |
23 | protected Properties props; |
26 | protected Authenticator auth; |
24 | protected Authenticator auth; |
27 | 25 | ||
28 | private String smtpHost = "localhost"; |
26 | private String smtpHost = "localhost"; |
29 | private String smtpUsername = ""; |
27 | private String smtpUsername = ""; |
30 | private String smtpPassword = ""; |
28 | private String smtpPassword = ""; |
31 | private int smtpPort = 25; |
29 | private int smtpPort = 25; |
32 | private boolean smtpAuth = false; |
30 | private boolean smtpAuth = false; |
33 | // private String smtpAuthUser = ""; |
31 | // private String smtpAuthUser = ""; |
34 | // private String smtpAuthPass = ""; |
32 | // private String smtpAuthPass = ""; |
35 | private String recipient = ""; |
33 | private String recipient = ""; |
36 | private String subject = ""; |
34 | private String subject = ""; |
37 | private String mailFrom = ""; |
35 | private String mailFrom = ""; |
38 | 36 | ||
39 | public String getMailFrom() { |
37 | public String getMailFrom() { |
40 | return mailFrom; |
38 | return mailFrom; |
41 | } |
39 | } |
42 | 40 | ||
43 | public void setMailFrom(String mailFrom) throws InvalidMailAddressException { |
41 | public void setMailFrom(String mailFrom) { |
44 | if (mailFrom == null) throw new InvalidMailAddressException(); |
- | |
45 | this.mailFrom = mailFrom.trim(); |
42 | this.mailFrom = mailFrom.trim(); |
46 | } |
43 | } |
47 | 44 | ||
48 | public String getRecipient() { |
45 | public String getRecipient() { |
49 | return recipient; |
46 | return recipient; |
50 | } |
47 | } |
51 | 48 | ||
52 | public void setRecipient(String recipient) throws InvalidMailAddressException { |
49 | public void setRecipient(String recipient) { |
53 | if (recipient == null) throw new InvalidMailAddressException(); |
- | |
54 | this.recipient = recipient.trim(); |
50 | this.recipient = recipient.trim(); |
55 | } |
51 | } |
56 | 52 | ||
57 | public String getSubject() { |
53 | public String getSubject() { |
58 | return subject; |
54 | return subject; |
59 | } |
55 | } |
60 | 56 | ||
61 | public void setSubject(String subject) { |
57 | public void setSubject(String subject) { |
62 | this.subject = subject; |
58 | this.subject = subject; |
63 | if (this.subject == null) |
59 | if (this.subject == null) |
64 | this.subject = ""; |
60 | this.subject = ""; |
65 | } |
61 | } |
66 | 62 | ||
67 | public String getSmtpHost() { |
63 | public String getSmtpHost() { |
68 | return smtpHost; |
64 | return smtpHost; |
69 | } |
65 | } |
70 | 66 | ||
71 | public void setSmtpHost(String smtpHost) { |
67 | public void setSmtpHost(String smtpHost) { |
72 | this.smtpHost = smtpHost; |
68 | this.smtpHost = smtpHost; |
73 | if (this.smtpHost == null) |
69 | if (this.smtpHost == null) |
74 | this.smtpHost = ""; |
70 | this.smtpHost = ""; |
75 | } |
71 | } |
76 | 72 | ||
77 | public String getSmtpUsername() { |
73 | public String getSmtpUsername() { |
78 | return smtpUsername; |
74 | return smtpUsername; |
79 | } |
75 | } |
80 | 76 | ||
81 | public void setSmtpUsername(String smtpUsername) { |
77 | public void setSmtpUsername(String smtpUsername) { |
82 | this.smtpUsername = smtpUsername; |
78 | this.smtpUsername = smtpUsername; |
83 | if (this.smtpUsername == null) |
79 | if (this.smtpUsername == null) |
84 | this.smtpUsername = ""; |
80 | this.smtpUsername = ""; |
85 | } |
81 | } |
86 | 82 | ||
87 | public String getSmtpPassword() { |
83 | public String getSmtpPassword() { |
88 | return smtpPassword; |
84 | return smtpPassword; |
89 | } |
85 | } |
90 | 86 | ||
91 | public void setSmtpPassword(String smtpPassword) { |
87 | public void setSmtpPassword(String smtpPassword) { |
92 | this.smtpPassword = smtpPassword; |
88 | this.smtpPassword = smtpPassword; |
93 | if (this.smtpPassword == null) |
89 | if (this.smtpPassword == null) |
94 | this.smtpPassword = ""; |
90 | this.smtpPassword = ""; |
95 | } |
91 | } |
96 | 92 | ||
97 | public boolean isSmtpAuth() { |
93 | public boolean isSmtpAuth() { |
98 | return smtpAuth; |
94 | return smtpAuth; |
99 | } |
95 | } |
100 | 96 | ||
101 | public void setSmtpAuth(boolean smtpAuth) { |
97 | public void setSmtpAuth(boolean smtpAuth) { |
102 | this.smtpAuth = smtpAuth; |
98 | this.smtpAuth = smtpAuth; |
103 | } |
99 | } |
104 | 100 | ||
105 | // public String getSmtpAuthUser() { |
101 | // public String getSmtpAuthUser() { |
106 | // return smtpAuthUser; |
102 | // return smtpAuthUser; |
107 | // } |
103 | // } |
108 | // |
104 | // |
109 | // public void setSmtpAuthUser(String smtpAuthUser) { |
105 | // public void setSmtpAuthUser(String smtpAuthUser) { |
110 | // this.smtpAuthUser = smtpAuthUser; |
106 | // this.smtpAuthUser = smtpAuthUser; |
111 | // if (this.smtpAuthUser == null) this.smtpAuthUser = ""; |
107 | // if (this.smtpAuthUser == null) this.smtpAuthUser = ""; |
112 | // } |
108 | // } |
113 | // |
109 | // |
114 | // public String getSmtpAuthPass() { |
110 | // public String getSmtpAuthPass() { |
115 | // return smtpAuthPass; |
111 | // return smtpAuthPass; |
116 | // } |
112 | // } |
117 | // |
113 | // |
118 | // public void setSmtpAuthPass(String smtpAuthPass) { |
114 | // public void setSmtpAuthPass(String smtpAuthPass) { |
119 | // this.smtpAuthPass = smtpAuthPass; |
115 | // this.smtpAuthPass = smtpAuthPass; |
120 | // if (this.smtpAuthPass == null) this.smtpAuthPass = ""; |
116 | // if (this.smtpAuthPass == null) this.smtpAuthPass = ""; |
121 | // } |
117 | // } |
122 | 118 | ||
123 | public int getSmtpPort() { |
119 | public int getSmtpPort() { |
124 | return smtpPort; |
120 | return smtpPort; |
125 | } |
121 | } |
126 | 122 | ||
127 | public void setSmtpPort(int smtpPort) { |
123 | public void setSmtpPort(int smtpPort) { |
128 | this.smtpPort = smtpPort; |
124 | this.smtpPort = smtpPort; |
129 | } |
125 | } |
130 | 126 | ||
131 | protected void generateProperties() |
127 | protected void generateProperties() |
132 | throws AuthentificateDataIncompleteException { |
128 | throws AuthentificateDataIncompleteException { |
133 | props = new Properties(); |
129 | props = new Properties(); |
134 | 130 | ||
135 | // http://72.5.124.55/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html |
131 | // http://72.5.124.55/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html |
136 | if ((smtpHost != null) && (!"".equals(smtpHost))) { |
132 | if ((smtpHost != null) && (!"".equals(smtpHost))) { |
137 | props.put("mail.smtp.host", smtpHost); |
133 | props.put("mail.smtp.host", smtpHost); |
138 | } |
134 | } |
139 | props.setProperty("mail.transport.protocol", TRANSPORT_PROTOCOL); |
135 | props.setProperty("mail.transport.protocol", TRANSPORT_PROTOCOL); |
140 | props.setProperty("mail.smtp.port", "" + smtpPort); |
136 | props.setProperty("mail.smtp.port", "" + smtpPort); |
141 | if ((smtpUsername != null) && (!"".equals(smtpUsername))) { |
137 | if ((smtpUsername != null) && (!"".equals(smtpUsername))) { |
142 | props.setProperty("mail.smtp.user", smtpUsername); |
138 | props.setProperty("mail.smtp.user", smtpUsername); |
143 | } |
139 | } |
144 | 140 | ||
145 | props.put("mail.smtp.auth", smtpAuth); |
141 | props.put("mail.smtp.auth", smtpAuth); |
146 | if (smtpAuth) { |
142 | if (smtpAuth) { |
147 | String smtpAuthUser = smtpUsername; |
143 | String smtpAuthUser = smtpUsername; |
148 | String smtpAuthPass = smtpPassword; |
144 | String smtpAuthPass = smtpPassword; |
149 | 145 | ||
150 | if ((smtpAuthUser != null) && (!"".equals(smtpAuthUser))) { |
146 | if ((smtpAuthUser != null) && (!"".equals(smtpAuthUser))) { |
151 | if ((smtpAuthPass != null) && (!"".equals(smtpAuthPass))) { |
147 | if ((smtpAuthPass != null) && (!"".equals(smtpAuthPass))) { |
152 | auth = new SmtpAuthenticator(smtpAuthUser, smtpAuthPass); |
148 | auth = new SmtpAuthenticator(smtpAuthUser, smtpAuthPass); |
153 | } else { |
149 | } else { |
154 | throw new AuthentificateDataIncompleteException(); |
150 | throw new AuthentificateDataIncompleteException(); |
155 | } |
151 | } |
156 | } else { |
152 | } else { |
157 | throw new AuthentificateDataIncompleteException(); |
153 | throw new AuthentificateDataIncompleteException(); |
158 | } |
154 | } |
159 | } else { |
155 | } else { |
160 | auth = null; |
156 | auth = null; |
161 | } |
157 | } |
162 | 158 | ||
163 | if ((mailFrom != null) && (!"".equals(mailFrom))) { |
159 | if ((mailFrom != null) && (!"".equals(mailFrom))) { |
164 | props.setProperty("mail.smtp.from", mailFrom); |
160 | props.setProperty("mail.smtp.from", mailFrom); |
165 | } |
161 | } |
166 | } |
162 | } |
167 | 163 | ||
168 | protected void generateSession() { |
164 | protected void generateSession() { |
169 | // http://blog.dafuer.de/2006/08/22/javamail-access-to-default-session-denied/ |
165 | // http://blog.dafuer.de/2006/08/22/javamail-access-to-default-session-denied/ |
170 | // final Session session = Session.getDefaultInstance(props, auth); |
166 | // final Session session = Session.getDefaultInstance(props, auth); |
171 | session = Session.getInstance(props, auth); |
167 | session = Session.getInstance(props, auth); |
172 | } |
168 | } |
173 | 169 | ||
174 | protected void generateMailObject() throws MessagingException, |
170 | protected void generateMailObject() throws MessagingException, |
175 | AuthentificateDataIncompleteException { |
171 | AuthentificateDataIncompleteException { |
176 | 172 | ||
177 | /* final Message */msg = new MimeMessage(session); |
173 | /* final Message */msg = new MimeMessage(session); |
178 | 174 | ||
179 | final InternetAddress addressTo = new InternetAddress(recipient); |
175 | final InternetAddress addressTo = new InternetAddress(recipient); |
180 | msg.setRecipient(Message.RecipientType.TO, addressTo); |
176 | msg.setRecipient(Message.RecipientType.TO, addressTo); |
181 | msg.setSubject(subject); |
177 | msg.setSubject(subject); |
182 | 178 | ||
183 | if ((mailFrom != null) && (!"".equals(mailFrom))) { |
179 | if ((mailFrom != null) && (!"".equals(mailFrom))) { |
184 | final InternetAddress addressFrom = new InternetAddress(mailFrom); |
180 | final InternetAddress addressFrom = new InternetAddress(mailFrom); |
185 | msg.setFrom(addressFrom); |
181 | msg.setFrom(addressFrom); |
186 | } |
182 | } |
187 | } |
183 | } |
188 | 184 | ||
189 | protected void doSend() throws MessagingException { |
185 | protected void doSend() throws MessagingException { |
190 | Transport tr = session.getTransport(TRANSPORT_PROTOCOL); |
186 | Transport tr = session.getTransport(TRANSPORT_PROTOCOL); |
191 | tr.connect(smtpHost, smtpPort, smtpUsername, smtpPassword); |
187 | tr.connect(smtpHost, smtpPort, smtpUsername, smtpPassword); |
192 | msg.saveChanges(); // don't forget this |
188 | msg.saveChanges(); // don't forget this |
193 | tr.sendMessage(msg, msg.getAllRecipients()); |
189 | tr.sendMessage(msg, msg.getAllRecipients()); |
194 | tr.close(); |
190 | tr.close(); |
195 | } |
191 | } |
196 | 192 | ||
197 | public void sendMail() throws MessagingException, |
193 | public void sendMail() throws MessagingException, |
198 | AuthentificateDataIncompleteException { |
194 | AuthentificateDataIncompleteException { |
199 | 195 | ||
200 | // Damit die Funktionalität später erweitert oder verändert werden kann! |
196 | // Damit die Funktionalität später erweitert oder verändert werden kann! |
201 | generateProperties(); |
197 | generateProperties(); |
202 | generateSession(); |
198 | generateSession(); |
203 | generateMailObject(); |
199 | generateMailObject(); |
204 | doSend(); |
200 | doSend(); |
205 | } |
201 | } |
206 | } |
202 | } |