1 | package org.expeditee.agents.mail;
|
---|
2 |
|
---|
3 | import java.util.Date;
|
---|
4 | import java.util.Properties;
|
---|
5 |
|
---|
6 | import javax.mail.Authenticator;
|
---|
7 | import javax.mail.Message;
|
---|
8 | import javax.mail.PasswordAuthentication;
|
---|
9 | import javax.mail.Session;
|
---|
10 | import javax.mail.Transport;
|
---|
11 | import javax.mail.internet.InternetAddress;
|
---|
12 | import javax.mail.internet.MimeMessage;
|
---|
13 |
|
---|
14 | import org.expeditee.gui.AttributeValuePair;
|
---|
15 | import org.expeditee.gui.Frame;
|
---|
16 | import org.expeditee.gui.MessageBay;
|
---|
17 | import org.expeditee.items.Text;
|
---|
18 |
|
---|
19 | public class MailSession {
|
---|
20 | private static MailSession _theMailSession = null;
|
---|
21 |
|
---|
22 | private Session _session;
|
---|
23 |
|
---|
24 | private Transport _transport;
|
---|
25 |
|
---|
26 | private String _address;
|
---|
27 |
|
---|
28 | private MailSession(Frame settingsFrame) {
|
---|
29 | Properties props = System.getProperties();
|
---|
30 |
|
---|
31 | String username = null;
|
---|
32 | String password = "";
|
---|
33 |
|
---|
34 | // Set the settings
|
---|
35 | for (Text item : settingsFrame.getBodyTextItems(false)) {
|
---|
36 | AttributeValuePair avp = new AttributeValuePair(item.getText());
|
---|
37 | if (!avp.hasPair())
|
---|
38 | continue;
|
---|
39 | String attributeFullCase = avp.getAttribute();
|
---|
40 | String attribute = attributeFullCase.toLowerCase();
|
---|
41 |
|
---|
42 | if (attribute.equals("user")) {
|
---|
43 | username = avp.getValue();
|
---|
44 | props.setProperty("mail.user", username);
|
---|
45 | } else if (attribute.equals("password")) {
|
---|
46 | password = avp.getValue();
|
---|
47 | props.setProperty("mail.password", password);
|
---|
48 | } else if (attribute.equals("address")) {
|
---|
49 | _address = avp.getValue();
|
---|
50 | } else if (attribute.equals("smtpserver")) {
|
---|
51 | props.setProperty("mail.transport.protocol", "smtp");
|
---|
52 | props.setProperty("mail.host", avp.getValue());
|
---|
53 | props.setProperty("mail.smtp.starttls.enable", "true");
|
---|
54 | props.setProperty("mail.smtp.host", avp.getValue());
|
---|
55 | props.setProperty("mail.smtp.auth", "true");
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | // Create the authenticator
|
---|
60 | Authenticator auth = null;
|
---|
61 | if (username != null) {
|
---|
62 | auth = new SMTPAuthenticator(username, password);
|
---|
63 | }
|
---|
64 | java.security.Security
|
---|
65 | .addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
---|
66 |
|
---|
67 | // -- Attaching to default Session, or we could start a new one --
|
---|
68 | _session = Session.getDefaultInstance(props, auth);
|
---|
69 | try {
|
---|
70 | // -- Send the message --
|
---|
71 | _transport = _session.getTransport();
|
---|
72 | _transport.connect();
|
---|
73 | } catch (Exception e) {
|
---|
74 | MessageBay.errorMessage("Error in ExpMail setup");
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static boolean sendTextMessage(String to, String cc, String subject,
|
---|
79 | String body) {
|
---|
80 | if (_theMailSession == null) {
|
---|
81 | MessageBay.errorMessage("Add mail settings to profile frame");
|
---|
82 | return false;
|
---|
83 | } else if (to == null) {
|
---|
84 | MessageBay.errorMessage("Add tag @to:<sendToEmailAddress>");
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 |
|
---|
88 | try {
|
---|
89 | // -- Create a new message --
|
---|
90 | Message msg = new MimeMessage(_theMailSession._session);
|
---|
91 |
|
---|
92 | // -- Set the FROM and TO fields --
|
---|
93 | msg.setFrom(new InternetAddress(_theMailSession._address));
|
---|
94 | msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
|
---|
95 | to, false));
|
---|
96 |
|
---|
97 | // -- We could include CC recipients too --
|
---|
98 | if (cc != null) {
|
---|
99 | msg.setRecipients(Message.RecipientType.CC, InternetAddress
|
---|
100 | .parse(cc, false));
|
---|
101 | }
|
---|
102 |
|
---|
103 | // -- Set the subject and body text --
|
---|
104 | msg.setSubject(subject);
|
---|
105 | msg.setContent(body.toString(), "text/plain");
|
---|
106 |
|
---|
107 | // -- Set some other header information --
|
---|
108 | msg.setHeader("ExpMail", "Expeditee");
|
---|
109 | msg.setSentDate(new Date());
|
---|
110 |
|
---|
111 | Transport.send(msg, msg.getRecipients(Message.RecipientType.TO));
|
---|
112 | } catch (Exception e) {
|
---|
113 | MessageBay.errorMessage("Error sending mail: " + e.getMessage());
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | MessageBay.displayMessage("Message sent OK.");
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static void init(Frame settingsFrame) {
|
---|
121 |
|
---|
122 | if (settingsFrame == null)
|
---|
123 | return;
|
---|
124 |
|
---|
125 | if (_theMailSession == null)
|
---|
126 | _theMailSession = new MailSession(settingsFrame);
|
---|
127 | }
|
---|
128 |
|
---|
129 | private class SMTPAuthenticator extends javax.mail.Authenticator {
|
---|
130 | private String _username;
|
---|
131 |
|
---|
132 | private String _password;
|
---|
133 |
|
---|
134 | public SMTPAuthenticator(String username, String password) {
|
---|
135 | _username = username;
|
---|
136 | _password = password;
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public PasswordAuthentication getPasswordAuthentication() {
|
---|
141 | return new PasswordAuthentication(_username, _password);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public static void finalise() {
|
---|
146 | try {
|
---|
147 | if (_theMailSession._transport != null)
|
---|
148 | _theMailSession._transport.close();
|
---|
149 | } catch (Exception e) {
|
---|
150 |
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|