Rev 977 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package ak.kickup.core.mail;
import java.util.*;
import java.io.*;
import java.text.MessageFormat;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import ak.kickup.util.UserException;
import ak.kickup.util.StringConverter;
import ak.kickup.core.model.Participant;
public abstract class Messages
{
public static final String HOST = "localhost";
public static final String CHARSET = "windows-1251";
public static void send(String from, String to, String subject, String body)
throws UserException
{
try {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
InternetAddress[] address = { new InternetAddress(to) };
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject, CHARSET);
msg.setSentDate(new Date());
msg.setText(body, CHARSET);
Transport.send(msg);
}
catch(Exception ex) {
throw new UserException("ak.kickup.core.mail.send.error");
}
}
public static void sendRegistrationMessage(Participant participant, String ip)
throws UserException
{
prepareMail(participant, ip, "ak.kickup.core.mail.subject", "ak/kickup/core/RegistrationMail.txt");
}
public static void sendRemindMessage(Participant participant, String ip)
throws UserException
{
prepareMail(participant, ip, "ak.kickup.core.remindmail.subject", "ak/kickup/core/RemindMail.txt");
}
private static void prepareMail(Participant participant, String ip, String subjectKey, String messageFile)
throws UserException
{
String subject;
StringBuffer message = new StringBuffer();
// get subject
ResourceBundle res = ResourceBundle.getBundle("ak/kickup/core/CoreResources");
String subjectString = res.getString(subjectKey);
subject = MessageFormat.format(subjectString,
new String[] { participant.getEvent().getName() } );
// read message template
try {
ClassLoader cl = Messages.class.getClassLoader();
if(cl == null) cl = ClassLoader.getSystemClassLoader();
BufferedReader file = new BufferedReader(new InputStreamReader(cl.getResourceAsStream(
messageFile), "UTF-8"));
char[] buf = new char[2048];
int bufLen;
while((bufLen = file.read(buf)) >= 0) {
message.append(buf);
}
}
catch(Exception ex) {
throw new UserException("ak.kickup.core.mail.read.error");
}
// replace tokens
replace(message, "ident", participant.getIdent());
replace(message, "eventName", participant.getEvent().getName());
replace(message, "eventPrice",
StringConverter.toCurrency(participant.getEvent().getPrice()));
replace(message, "eventMoneyAccount", participant.getEvent().getMoneyAccount());
replace(message, "eventAdmins", participant.getEvent().getAdmins());
replace(message, "eventEmail", participant.getEvent().getEmail());
replace(message, "eventPhones", participant.getEvent().getPhones());
replace(message, "", "%");
// send message
send(participant.getEvent().getEmail(), participant.getEmail(),
subject, message.toString());
}
private static void replace(StringBuffer message, String token, String value)
{
if(value == null) value = "";
String s = "%" + token + "%";
int slen = s.length();
int vlen = value.length();
int pos = 0;
while((pos = message.indexOf(s, pos)) >= 0) {
message.replace(pos, pos + slen, value);
pos += vlen;
}
}
}