Rev 29 | Rev 970 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
30 | dev | 1 | package ak.kickup.core.mail; |
27 | dev | 2 | |
3 | import java.util.*; |
||
4 | import java.io.*; |
||
5 | import java.text.MessageFormat; |
||
6 | |||
7 | import javax.mail.Session; |
||
8 | import javax.mail.Message; |
||
9 | import javax.mail.Transport; |
||
10 | import javax.mail.internet.MimeMessage; |
||
11 | import javax.mail.internet.InternetAddress; |
||
12 | |||
13 | import ak.kickup.util.UserException; |
||
14 | import ak.kickup.util.StringConverter; |
||
15 | import ak.kickup.core.model.Participant; |
||
16 | |||
17 | public abstract class Messages |
||
18 | { |
||
19 | public static final String HOST = "localhost"; |
||
20 | public static final String CHARSET = "UTF8"; |
||
21 | |||
22 | public static void send(String from, String to, String subject, String body) |
||
23 | throws UserException |
||
24 | { |
||
25 | try { |
||
26 | Properties props = new Properties(); |
||
27 | props.put("mail.smtp.host", HOST); |
||
28 | |||
29 | Session session = Session.getInstance(props, null); |
||
30 | MimeMessage msg = new MimeMessage(session); |
||
31 | InternetAddress[] address = { new InternetAddress(to) }; |
||
32 | |||
33 | msg.setFrom(new InternetAddress(from)); |
||
34 | msg.setRecipients(Message.RecipientType.TO, address); |
||
35 | msg.setSubject(subject, CHARSET); |
||
36 | msg.setSentDate(new Date()); |
||
37 | msg.setText(body, CHARSET); |
||
38 | |||
39 | Transport.send(msg); |
||
40 | } |
||
41 | catch(Exception ex) { |
||
42 | throw new UserException("ak.kickup.core.mail.send.error"); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | public static void sendRegistrationMessage(Participant participant, String ip) |
||
47 | throws UserException |
||
48 | { |
||
49 | String subject; |
||
50 | StringBuffer message = new StringBuffer(); |
||
51 | |||
52 | // get subject |
||
53 | ResourceBundle res = ResourceBundle.getBundle("ak/kickup/core/CoreResources"); |
||
54 | String subjectString = res.getString("ak.kickup.core.mail.subject"); |
||
55 | subject = MessageFormat.format(subjectString, |
||
56 | new String[] { participant.getEvent().getName() } ); |
||
57 | |||
58 | // read message template |
||
59 | try { |
||
60 | ClassLoader cl = Messages.class.getClassLoader(); |
||
61 | if(cl == null) cl = ClassLoader.getSystemClassLoader(); |
||
62 | |||
63 | BufferedReader file = new BufferedReader(new InputStreamReader(cl.getResourceAsStream( |
||
64 | "ak/kickup/core/RegistrationMail.txt"), "UTF8")); |
||
65 | char[] buf = new char[2048]; |
||
66 | int bufLen; |
||
67 | while((bufLen = file.read(buf)) >= 0) { |
||
68 | message.append(buf); |
||
69 | } |
||
70 | } |
||
71 | catch(Exception ex) { |
||
72 | throw new UserException("ak.kickup.core.mail.read.error"); |
||
73 | } |
||
74 | |||
75 | // replace tokens |
||
76 | replace(message, "ident", participant.getIdent()); |
||
77 | replace(message, "eventName", participant.getEvent().getName()); |
||
78 | replace(message, "eventPrice", |
||
79 | StringConverter.toCurrency(participant.getEvent().getPrice())); |
||
80 | replace(message, "eventMoneyAccount", participant.getEvent().getMoneyAccount()); |
||
81 | replace(message, "eventAdmins", participant.getEvent().getAdmins()); |
||
82 | replace(message, "eventEmail", participant.getEvent().getEmail()); |
||
83 | replace(message, "", "%"); |
||
84 | |||
85 | // send message |
||
86 | send(participant.getEvent().getEmail(), participant.getEmail(), |
||
87 | subject, message.toString()); |
||
88 | } |
||
89 | |||
90 | private static void replace(StringBuffer message, String token, String value) |
||
91 | { |
||
29 | dev | 92 | if(value == null) value = ""; |
93 | |||
27 | dev | 94 | String s = "%" + token + "%"; |
95 | int slen = s.length(); |
||
96 | int vlen = value.length(); |
||
97 | int pos = 0; |
||
98 | while((pos = message.indexOf(s, pos)) >= 0) { |
||
99 | message.replace(pos, pos + slen, value); |
||
100 | pos += vlen; |
||
101 | } |
||
102 | } |
||
103 | } |