Subversion Repositories general

Rev

Rev 29 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.kickup.core.action;

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 = "UTF8";

        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
        {
                String       subject;
                StringBuffer message = new StringBuffer();

                // get subject
                ResourceBundle res = ResourceBundle.getBundle("ak/kickup/core/CoreResources");
        String subjectString = res.getString("ak.kickup.core.mail.subject");
        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(
                                "ak/kickup/core/RegistrationMail.txt"), "UTF8"));
                        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, "",                  "%");

        // send message
        send(participant.getEvent().getEmail(), participant.getEmail(),
                subject, message.toString());
        }

        private static void replace(StringBuffer message, String token, String 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;
                }
        }
}