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