Subversion Repositories general

Rev

Rev 977 | 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
{
978 dev 19
	public static final String HOST    = "localhost";
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);
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);
971 dev 35
		    msg.setSubject(subject, CHARSET);
27 dev 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
	{
977 dev 49
		prepareMail(participant, ip, "ak.kickup.core.mail.subject", "ak/kickup/core/RegistrationMail.txt");
50
	}
51
 
52
	public static void sendRemindMessage(Participant participant, String ip)
53
		throws UserException
54
	{
55
		prepareMail(participant, ip, "ak.kickup.core.remindmail.subject", "ak/kickup/core/RemindMail.txt");
56
	}
57
 
58
	private static void prepareMail(Participant participant, String ip, String subjectKey, String messageFile)
59
		throws UserException
60
	{
27 dev 61
		String       subject;
62
		StringBuffer message = new StringBuffer();
63
 
64
		// get subject
65
		ResourceBundle res = ResourceBundle.getBundle("ak/kickup/core/CoreResources");
977 dev 66
        String subjectString = res.getString(subjectKey);
27 dev 67
        subject = MessageFormat.format(subjectString,
68
        	new String[] { participant.getEvent().getName() } );
69
 
70
		// read message template
71
		try {
72
			ClassLoader cl = Messages.class.getClassLoader();
73
			if(cl == null) cl = ClassLoader.getSystemClassLoader();
74
 
75
			BufferedReader file = new BufferedReader(new InputStreamReader(cl.getResourceAsStream(
977 dev 76
				messageFile), "UTF-8"));
27 dev 77
			char[] buf = new char[2048];
78
			int bufLen;
79
			while((bufLen = file.read(buf)) >= 0) {
80
				message.append(buf);
81
			}
82
		}
83
		catch(Exception ex) {
84
			throw new UserException("ak.kickup.core.mail.read.error");
85
		}
86
 
87
		// replace tokens
88
        replace(message, "ident",             participant.getIdent());
89
        replace(message, "eventName",         participant.getEvent().getName());
90
        replace(message, "eventPrice",
91
        	StringConverter.toCurrency(participant.getEvent().getPrice()));
92
        replace(message, "eventMoneyAccount", participant.getEvent().getMoneyAccount());
93
        replace(message, "eventAdmins",       participant.getEvent().getAdmins());
94
        replace(message, "eventEmail",        participant.getEvent().getEmail());
972 dev 95
        replace(message, "eventPhones",       participant.getEvent().getPhones());
27 dev 96
        replace(message, "",                  "%");
97
 
98
        // send message
99
        send(participant.getEvent().getEmail(), participant.getEmail(),
100
        	subject, message.toString());
101
	}
102
 
103
	private static void replace(StringBuffer message, String token, String value)
104
	{
29 dev 105
		if(value == null) value = "";
106
 
27 dev 107
		String s = "%" + token + "%";
108
		int slen = s.length();
109
		int vlen = value.length();
110
		int pos = 0;
111
		while((pos = message.indexOf(s, pos)) >= 0) {
112
			message.replace(pos, pos + slen, value);
113
			pos += vlen;
114
		}
115
	}
116
}