1 package de.tivsource.page.helper.sender;
2
3
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.text.MessageFormat;
9 import java.text.SimpleDateFormat;
10 import java.util.Properties;
11
12 import javax.activation.DataHandler;
13 import javax.activation.DataSource;
14 import javax.activation.FileDataSource;
15 import javax.mail.Authenticator;
16 import javax.mail.Message;
17 import javax.mail.MessagingException;
18 import javax.mail.PasswordAuthentication;
19 import javax.mail.Session;
20 import javax.mail.Transport;
21 import javax.mail.internet.InternetAddress;
22 import javax.mail.internet.MimeBodyPart;
23 import javax.mail.internet.MimeMessage;
24 import javax.mail.internet.MimeMultipart;
25 import javax.mail.internet.MimeUtility;
26
27 import org.apache.pdfbox.exceptions.COSVisitorException;
28
29 import de.tivsource.page.entity.reservation.Reservation;
30 import de.tivsource.page.helper.EmailTemplate;
31 import de.tivsource.page.helper.pdf.CreateReservationPDF;
32
33 public class ReservationMail {
34
35
36
37
38 private String username;
39
40
41
42
43 private String password;
44
45
46
47
48 private Authenticator auth = new Authenticator() {
49 @Override
50 public PasswordAuthentication getPasswordAuthentication() {
51 return new PasswordAuthentication(username, password);
52 }
53 };
54
55
56
57
58 private InputStream template;
59
60
61
62
63 private URL templatePath;
64
65
66
67
68 private Reservation reservation;
69
70 private File logoFile;
71 private File adFile;
72
73 private File fontFile;
74
75
76
77
78 private Session session;
79
80 private EmailTemplate emailTemplate;
81
82 private String fromAddress;
83 private String fromName;
84
85 private String replyToAddress;
86 private String replyToName;
87
88 private String bccAddress;
89
90 public ReservationMail(String username, String password, URL templatePath,
91 Reservation reservation, File logoFile, File adFile,
92 Properties properties, File fontFile, String fromAddress,
93 String fromName, String replyToAddress, String replyToName, String bccAddress)
94 throws IOException {
95 super();
96 this.username = username;
97 this.password = password;
98 this.templatePath = templatePath;
99 this.reservation = reservation;
100 this.logoFile = logoFile;
101 this.adFile = adFile;
102 this.fontFile = fontFile;
103 this.fromAddress = fromAddress;
104 this.fromName = fromName;
105 this.replyToAddress = replyToAddress;
106 this.replyToName = replyToName;
107 this.bccAddress = bccAddress;
108 this.template = this.templatePath.openStream();
109 this.session = Session.getInstance(properties, auth);
110 this.session.setDebug(false);
111 this.emailTemplate = (EmailTemplate)EmailTemplate.getEmailTemplate(template);
112 }
113
114 public void send() throws MessagingException, COSVisitorException, IOException {
115
116
117 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm dd-MM-yyyy ");
118
119 String[] arguments = {
120 reservation.getGender() ? "Sehr geehrte Frau" : "Sehr geehrter Herr",
121 reservation.getFirstname(),
122 reservation.getLastname(),
123 replyToName,
124 reservation.getEmail(),
125 reservation.getTelephone(),
126 simpleDateFormat.format(reservation.getTime()),
127 reservation.getQuantity().toString(),
128 reservation.getWishes(),
129 reservation.getWishes().replace("\n", "<br/>")
130 };
131
132
133
134
135 MimeMessage message = new MimeMessage(session);
136
137
138
139
140 message.setFrom(new InternetAddress(
141 fromAddress,
142 MimeUtility.encodeText(fromName, "UTF-8", "Q")
143 ));
144
145 message.addRecipients(Message.RecipientType.TO,
146 new javax.mail.Address[]{
147 new InternetAddress(
148 reservation.getEmail(),
149 MimeUtility.encodeText(reservation.getFirstname() +" "+ reservation.getLastname(),"UTF-8", "Q")
150 )
151 }
152 );
153
154 message.addRecipients(Message.RecipientType.CC,
155 emailTemplate.getCcAddresses());
156 message.addRecipients(Message.RecipientType.BCC,
157 new javax.mail.Address[]{
158 new InternetAddress(
159 bccAddress,
160 MimeUtility.encodeText("BCC Empfänger","UTF-8", "Q")
161 )
162 }
163 );
164
165
166 message.setReplyTo(new javax.mail.Address[]{
167 new InternetAddress(
168 replyToAddress,
169 MimeUtility.encodeText(replyToName, "UTF-8", "Q")
170 )
171 });
172
173
174
175
176 message.setSubject(
177
178 MimeUtility.encodeText(
179
180 MessageFormat.format(emailTemplate.getSubject(), (Object[])arguments),"UTF-8", "Q"),
181 "UTF-8");
182
183
184
185
186
187 MimeMultipart mantle = new MimeMultipart("mixed");
188
189
190
191
192
193
194 MimeMultipart cover = new MimeMultipart("alternative");
195
196
197 MimeBodyPart text = new MimeBodyPart();
198
199
200 cover.addBodyPart(text);
201
202
203 MimeBodyPart html = new MimeBodyPart();
204
205
206 cover.addBodyPart(html);
207
208
209
210 MimeBodyPart coverMimeBodyPart = new MimeBodyPart();
211 coverMimeBodyPart.setContent(cover);
212
213
214 mantle.addBodyPart(coverMimeBodyPart);
215
216
217
218
219
220 html.setContent(MessageFormat.format(emailTemplate.getHtml(), (Object[])arguments),
221 "text/html; charset=UTF-8");
222 html.setHeader("Content-Transfer-Encoding", "8bit");
223
224
225
226
227
228 text.setText(MessageFormat.format(emailTemplate.getBody(), (Object[])arguments));
229 text.setHeader("Content-Transfer-Encoding", "8bit");
230
231
232 message.setContent(mantle);
233
234
235 File pdfFile = new File("/tmp/" + reservation.getUuid() + ".pdf");
236 new CreateReservationPDF(
237 pdfFile,
238 reservation,
239 logoFile,
240 adFile,
241 fontFile
242 );
243
244
245 MimeBodyPart mimeBodyPart = new MimeBodyPart();
246
247
248 DataSource source = new FileDataSource(pdfFile);
249 mimeBodyPart.setDataHandler(new DataHandler(source));
250 mimeBodyPart.setHeader("Content-Type", "application/pdf");
251 mimeBodyPart.setFileName("Reservierungsbestaetigung.pdf");
252
253
254 mantle.addBodyPart(mimeBodyPart);
255
256
257 message.setHeader("Content-Transfer-Encoding", "8bit");
258
259
260 message.saveChanges();
261
262
263 Transport.send(message);
264
265
266 pdfFile.delete();
267
268 }
269
270 }