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
11
12 public class Email {
13
14
15
16
17
18
19
20
21
22
23
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
38
39
40
41
42
43
44
45
46
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
71
72
73
74
75
76
77
78
79
80
81
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
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 }