Rev 978 | 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 | { |
||
1265 | dev | 19 | public static final String HOST = "192.168.10.1"; |
976 | dev | 20 | public static final String CHARSET = "windows-1251"; |
27 | dev | 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); |
||
1265 | dev | 28 | props.put("mail.smtp.localhost", "26th.net"); |
27 | dev | 29 | |
30 | Session session = Session.getInstance(props, null); |
||
31 | MimeMessage msg = new MimeMessage(session); |
||
32 | InternetAddress[] address = { new InternetAddress(to) }; |
||
33 | |||
34 | msg.setFrom(new InternetAddress(from)); |
||
35 | msg.setRecipients(Message.RecipientType.TO, address); |
||
971 | dev | 36 | msg.setSubject(subject, CHARSET); |
27 | dev | 37 | msg.setSentDate(new Date()); |
38 | msg.setText(body, CHARSET); |
||
39 | |||
40 | Transport.send(msg); |
||
41 | } |
||
42 | catch(Exception ex) { |
||
1265 | dev | 43 | ex.printStackTrace(); |
27 | dev | 44 | throw new UserException("ak.kickup.core.mail.send.error"); |
45 | } |
||
46 | } |
||
47 | |||
48 | public static void sendRegistrationMessage(Participant participant, String ip) |
||
49 | throws UserException |
||
50 | { |
||
977 | dev | 51 | prepareMail(participant, ip, "ak.kickup.core.mail.subject", "ak/kickup/core/RegistrationMail.txt"); |
52 | } |
||
53 | |||
54 | public static void sendRemindMessage(Participant participant, String ip) |
||
55 | throws UserException |
||
56 | { |
||
57 | prepareMail(participant, ip, "ak.kickup.core.remindmail.subject", "ak/kickup/core/RemindMail.txt"); |
||
58 | } |
||
59 | |||
60 | private static void prepareMail(Participant participant, String ip, String subjectKey, String messageFile) |
||
61 | throws UserException |
||
62 | { |
||
27 | dev | 63 | String subject; |
64 | StringBuffer message = new StringBuffer(); |
||
65 | |||
66 | // get subject |
||
67 | ResourceBundle res = ResourceBundle.getBundle("ak/kickup/core/CoreResources"); |
||
977 | dev | 68 | String subjectString = res.getString(subjectKey); |
27 | dev | 69 | subject = MessageFormat.format(subjectString, |
70 | new String[] { participant.getEvent().getName() } ); |
||
71 | |||
72 | // read message template |
||
73 | try { |
||
74 | ClassLoader cl = Messages.class.getClassLoader(); |
||
75 | if(cl == null) cl = ClassLoader.getSystemClassLoader(); |
||
76 | |||
77 | BufferedReader file = new BufferedReader(new InputStreamReader(cl.getResourceAsStream( |
||
977 | dev | 78 | messageFile), "UTF-8")); |
27 | dev | 79 | char[] buf = new char[2048]; |
80 | int bufLen; |
||
81 | while((bufLen = file.read(buf)) >= 0) { |
||
82 | message.append(buf); |
||
83 | } |
||
84 | } |
||
85 | catch(Exception ex) { |
||
86 | throw new UserException("ak.kickup.core.mail.read.error"); |
||
87 | } |
||
88 | |||
89 | // replace tokens |
||
90 | replace(message, "ident", participant.getIdent()); |
||
91 | replace(message, "eventName", participant.getEvent().getName()); |
||
92 | replace(message, "eventPrice", |
||
93 | StringConverter.toCurrency(participant.getEvent().getPrice())); |
||
94 | replace(message, "eventMoneyAccount", participant.getEvent().getMoneyAccount()); |
||
95 | replace(message, "eventAdmins", participant.getEvent().getAdmins()); |
||
96 | replace(message, "eventEmail", participant.getEvent().getEmail()); |
||
972 | dev | 97 | replace(message, "eventPhones", participant.getEvent().getPhones()); |
27 | dev | 98 | replace(message, "", "%"); |
99 | |||
100 | // send message |
||
101 | send(participant.getEvent().getEmail(), participant.getEmail(), |
||
102 | subject, message.toString()); |
||
103 | } |
||
104 | |||
105 | private static void replace(StringBuffer message, String token, String value) |
||
106 | { |
||
29 | dev | 107 | if(value == null) value = ""; |
108 | |||
27 | dev | 109 | String s = "%" + token + "%"; |
110 | int slen = s.length(); |
||
111 | int vlen = value.length(); |
||
112 | int pos = 0; |
||
113 | while((pos = message.indexOf(s, pos)) >= 0) { |
||
114 | message.replace(pos, pos + slen, value); |
||
115 | pos += vlen; |
||
116 | } |
||
117 | } |
||
118 | } |