View Javadoc

1   package org.paneris.jal.model;
2   
3   import java.io.PrintStream;
4   import java.text.SimpleDateFormat;
5   import java.util.Date;
6   
7   import sun.net.smtp.SmtpClient;
8   
9   /**
10   * Send an email to one or more recipients.
11   */
12  public class Email {
13  
14    /**
15     * Send an email.
16     * 
17     * @param database   our database name
18     * @param fromEmail  email from
19     * @param to         email (and name possibly) to 
20     * @param replyto    who to reply to (possibly "")
21     * @param subject    message subject
22     * @param message    message body
23     * @throws Exception if anything goes wrong
24     */
25    public static void send(
26        String database,
27        String fromEmail,
28        String to,
29        String replyto,
30        String subject,
31        String message) throws Exception {
32  
33      send(database, "", fromEmail, to, replyto, subject, message);
34    }
35  
36    /**
37     * Send an email.
38     * 
39     * @param database   our database name
40     * @param fromName   name email from 
41     * @param fromEmail  email from
42     * @param to         email (and name possibly) to 
43     * @param replyto    who to reply to (possibly "")
44     * @param subject    message subject
45     * @param message    message body
46     * @throws Exception if anything goes wrong
47     */
48    public static void send(
49        String database,
50        String fromName,
51        String fromEmail,
52        String to,
53        String replyto,
54        String subject,
55        String message) throws Exception {
56  
57      String[] toList = { to };
58      sendToList(
59        database,
60        fromName,
61        fromEmail,
62        toList,
63        to,
64        replyto,
65        subject,
66        message);
67    }
68  
69    /**
70     * Send an email to a list of recipients.
71     * 
72     * @param database   our database name
73     * @param fromName   name email from 
74     * @param fromEmail  email from
75     * @param to         email (and name possibly) to 
76     * @param toList     list of emails (possibly names) comma separated
77     * @param apparentlyTo goes in To field
78     * @param replyto    who to reply to (possibly "")
79     * @param subject    message subject
80     * @param message    message body
81     * @throws Exception if anything goes wrong
82     */
83    public static void sendToList(
84        String database,
85        String fromName,
86        String fromEmail,
87        String[] toList,
88        String apparentlyTo,
89        String replyto,
90        String subject,
91        String message) throws Exception {
92  
93      SmtpClient smtp;
94      String smtpserver = "";
95      try {
96        try {
97          SystemProperties sp = new SystemProperties(database);
98          smtpserver = sp.getProperty("smtpserver");
99        }
100       catch (Exception e) {
101         smtpserver = "ganesh.paneris.org";
102       }
103       System.getProperties().put("mail.host", smtpserver);
104       smtp = new SmtpClient(smtpserver);
105     }
106     catch (Exception e) {
107       throw new EmailException(
108         "Couldn't create smtp client " + smtpserver + ", " + e.toString());
109     }
110 
111     // construct the data
112     Date now = new Date();
113     SimpleDateFormat formatter =
114       new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
115     String nowString = formatter.format(now);
116     smtp.from(fromEmail);
117     for (int i = 0; i < toList.length; i++)
118       smtp.to(toList[i]);
119     PrintStream msg = smtp.startMessage();
120     String fromString = fromName + " <" + fromEmail + ">";
121     msg.println("Date: " + nowString);
122     if (!replyto.equals(""))
123       msg.println("Reply-to: " + replyto);
124     msg.println("From: " + fromString);
125     msg.println("Subject: " + subject);
126     msg.println("To: " + apparentlyTo);
127     msg.println();
128     msg.println(message);
129     smtp.closeServer();
130   }
131 
132 }