| 1 | |
package org.paneris.jammyjoes.mail; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.util.Enumeration; |
| 6 | |
import java.util.Properties; |
| 7 | |
|
| 8 | |
import javax.mail.MessagingException; |
| 9 | |
import javax.mail.Session; |
| 10 | |
|
| 11 | |
import org.paneris.jammyjoes.model.User; |
| 12 | |
|
| 13 | |
import com.quiotix.html.parser.ParseException; |
| 14 | |
import com.sun.mail.smtp.SMTPTransport; |
| 15 | |
|
| 16 | |
public class MailFascade implements Sender { |
| 17 | |
|
| 18 | |
|
| 19 | |
private Session session; |
| 20 | |
private MailTransport transport; |
| 21 | 0 | private Properties props = new Properties(); |
| 22 | |
|
| 23 | |
|
| 24 | 0 | public MailFascade(Session session, MailTransport transport) { |
| 25 | 0 | this.session = session; |
| 26 | 0 | this.transport = transport; |
| 27 | 0 | } |
| 28 | |
|
| 29 | 0 | public MailFascade(String smtpServer) { |
| 30 | 0 | props.put("mail.smtp.host", smtpServer); |
| 31 | 0 | this.session = makeSession(); |
| 32 | 0 | this.transport = new TransportAbapter(new SMTPTransport(session, null)); |
| 33 | 0 | } |
| 34 | |
|
| 35 | |
public void send(MimeMail mail) throws MessagingException, ParseException, IOException { |
| 36 | 0 | transport.send(mail); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
public Newsletter createNewsletter(String directory, String name) throws Exception { |
| 40 | 0 | return new Newsletter(session, directory, name); |
| 41 | |
} |
| 42 | |
|
| 43 | |
public HtmlMail createHtmlMail(String content) throws Exception { |
| 44 | 0 | return new HtmlMail(session, content); |
| 45 | |
} |
| 46 | |
|
| 47 | |
private Session makeSession() { |
| 48 | 0 | Session session = Session.getDefaultInstance(props); |
| 49 | 0 | return session; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public String getProperty(String name) { |
| 53 | 0 | return props.getProperty(name); |
| 54 | |
} |
| 55 | |
|
| 56 | |
public int mailPeople(Enumeration users, Writer out, Newsletter newsletter) throws IOException { |
| 57 | 0 | int count=0; |
| 58 | 0 | while (users.hasMoreElements()) { |
| 59 | 0 | User user = (User) users.nextElement(); |
| 60 | 0 | if (sendToUser(out, user, newsletter)) { |
| 61 | 0 | count++; |
| 62 | |
} |
| 63 | 0 | out.flush(); |
| 64 | 0 | } |
| 65 | 0 | return count; |
| 66 | |
} |
| 67 | |
|
| 68 | |
private boolean sendToUser(Writer out, User user, Newsletter newsletter) throws IOException { |
| 69 | 0 | if (user.getSpam().booleanValue()) { |
| 70 | 0 | String address = user.getEmail(); |
| 71 | 0 | out.write(user.getId() + " " + address); |
| 72 | 0 | out.flush(); |
| 73 | |
try { |
| 74 | 0 | newsletter.setRecipient(address); |
| 75 | 0 | send(newsletter); |
| 76 | 0 | out.write("....Sent<br>\n"); |
| 77 | 0 | return true; |
| 78 | 0 | } catch (Exception e) { |
| 79 | 0 | out.write("....Failed: " + e + "<br>\n"); |
| 80 | 0 | return false; |
| 81 | |
} |
| 82 | |
} |
| 83 | 0 | return false; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
} |