View Javadoc

1   /* $Id: Mailer.java,v 1.2 2006/01/11 16:10:36 viciaq Exp $ */
2   package net.sourceforge.jdbdump.connect;
3   
4   import java.io.FileInputStream;
5   import java.util.Date;
6   import java.util.Properties;
7   
8   import javax.mail.Message;
9   import javax.mail.MessagingException;
10  import javax.mail.NoSuchProviderException;
11  import javax.mail.Session;
12  import javax.mail.Transport;
13  import javax.mail.internet.AddressException;
14  import javax.mail.internet.InternetAddress;
15  import javax.mail.internet.MimeMessage;
16  
17  import org.apache.log4j.Logger;
18  
19  /***
20   * Class handling notification emails.
21   * <br /><br />
22   * Mailer is used to send emails informing the user that either a backup file
23   * has been created and is ready for downloading, or a database has been
24   * completely restored from an uploaded backup.
25   * <br /><br />
26   * Necessary data is read from a properties file 'app.properties', which
27   * should contain:
28   * <ul>
29   * <li>Mailer.smtpserver - address of the server on which SMTP service is
30   *     running</li>
31   * <li>Mailer.login, Mailer.password - needed to authenticate the SMTP user</li>
32   * <li>Mailer.from - address appearing in "From:" header in sent emails</li>
33   * <li>Mailer.message.subject - the subject of sent emails</li>
34   * <li>Mailer.message.backup - the contents of the email sent to notify
35   *     about completed backup</li>
36   * <li>Mailer.message.restore - the contents of the email sent to notify
37   *     about completed restore</li>
38   * </ul>
39   *
40   * @author jsuder
41   */
42  
43  
44  public class Mailer {
45  	private Properties props;
46  	private InternetAddress fromAddress;
47  	private InternetAddress toAddress;
48  	private String server;
49  	private String login;
50  	private String password;
51  	private String subject;
52  	private String backupMessage;
53  	private String restoreMessage;
54  	
55  	private Logger logger = Logger.getLogger(Mailer.class);
56  	
57  	/***
58  	 * Create a new mailer. All necessary information is read from
59  	 * app.properties file.
60  	 */
61  	
62  	public Mailer() {
63  		props = System.getProperties();
64  		props.put("mail.smtp.auth", "true");
65  		
66  		Properties myprops = new Properties();
67  		try {
68  			myprops.load(new FileInputStream("app.properties"));
69  		} catch (Exception e) {
70  			logger.error("Error while reading mailer properties: " + e);
71  			return;
72  		}
73  		String from = myprops.getProperty("Mailer.from");
74  		String to;
75  		/*try {
76  			to = Configuration.getInstance().getEmail();
77  		} catch (Exception e) {
78  			logger.error("Error while loading configuration: " + e);
79  			return;
80  		}*/
81  		to = "viciu@vrak.krakow.pl";
82  		
83  		server = myprops.getProperty("Mailer.smtpserver");
84  		login = myprops.getProperty("Mailer.login");
85  		password = myprops.getProperty("Mailer.password");
86  		subject = myprops.getProperty("Mailer.message.subject");
87  		backupMessage = myprops.getProperty("Mailer.message.backup");
88  		restoreMessage = myprops.getProperty("Mailer.message.restore");
89  		
90  		if (from == null) {
91  			logger.error("Property Mailer.from not found.");
92  		} else if (server == null) {
93  			logger.error("Property Mailer.smtpserver not found.");
94  		} else if (login == null) {
95  			logger.error("Property Mailer.login not found.");
96  		} else if (password == null) {
97  			logger.error("Property Mailer.password not found.");
98  		} else if (subject == null) {
99  			logger.error("Property Mailer.message.subject not found.");
100 		} else if (backupMessage == null) {
101 			logger.error("Property Mailer.message.backup not found.");
102 		} else if (restoreMessage == null) {
103 			logger.error("Property Mailer.message.restore not found.");
104 		}
105 		
106 		try {
107 			fromAddress = new InternetAddress(from);
108 			toAddress = new InternetAddress(to);
109 		} catch (AddressException e) {
110 			logger.error("AddressException while loading from/to addresses "
111 				+ "for notification: " + e);
112 			return;
113 		}
114 	}
115 		
116 	/***
117 	 * Sends notification message to the user with specified contents.
118 	 * @param backupName name of the backup file, which is used to replace
119 	 *     all occurences of a '%f' string in the mail contents
120 	 * @param name mail contents  
121 	 */
122 
123 	protected void sendNotification(String backupName, String message) {
124 		Session mailSession = Session.getDefaultInstance(props, null);
125 		Message msg;
126 		
127 		logger.info("Preparing notification message...");
128 		try {
129 			msg = new MimeMessage(mailSession);
130 			msg.setFrom(fromAddress);
131 			msg.setRecipient(Message.RecipientType.TO, toAddress);
132 			msg.setSubject(subject);
133 			msg.setText(message.replaceAll("%f", backupName));
134 			msg.setSentDate(new Date());
135 		} catch (AddressException ae) {
136 			logger.error("AddressException while preparing notification "
137 				+ "message: " + ae);
138 			return;
139 		} catch (MessagingException me) {
140 			logger.error("MessagingException while preparing notification "
141 				+ "message: " + me);
142 			return;
143 		}
144 
145 		try {
146 			logger.info("Sending notification message...");
147 			Transport trans = mailSession.getTransport("smtp");
148 			trans.connect(server, login, password);
149 			msg.saveChanges();
150 			trans.sendMessage(msg, msg.getAllRecipients());
151 			trans.close();
152 			logger.info("Notification message has been sent.");
153 		} catch (NoSuchProviderException nspe) {
154 			logger.error("NoSuchProviderException while sending "
155 				+ "notification message: " + nspe);
156 			return;
157 		} catch (MessagingException me) {
158 			logger.error("MessagingException while sending "
159 					+ "notification message: " + me);
160 			return;
161 		}
162 	}
163 	
164 	/***
165 	 * Sends a notification message informing about a finished backup.
166 	 * @param backupName name of the backup file, which is used to replace
167 	 *     all occurences of a '%f' string in the mail contents
168 	 */
169 
170 	public void sendBackupNotification(String backupName) {
171 		sendNotification(backupName, backupMessage);
172 	}
173 	
174 	/***
175 	 * Sends a notification message informing about a finished restore.
176 	 * @param backupName name of the backup file, which is used to replace
177 	 *     all occurences of a '%f' string in the mail contents
178 	 */
179 
180 	public void sendRestoreNotification(String backupName) {
181 		sendNotification(backupName, restoreMessage);
182 	}
183 	
184 	public static void main(String[] args) {
185 		Mailer m = new Mailer();
186 		m.sendRestoreNotification("qqq.zip");
187 	}
188 }