View Javadoc
1   /******************************************************************************
2    * MailSender - This object sends validation emails to users
3    * 
4    * BSAccountMan - BuckoSoft Web Account Manager 
5    * Copyright(c) 2007 - Dick Balaska and BuckoSoft, Corp.
6    * 
7    */
8   
9   package com.buckosoft.BSAccountMan.mail;
10  
11  import java.util.Date;
12  import java.util.Properties;
13  
14  import javax.mail.Message;
15  import javax.mail.Session;
16  import javax.mail.Transport;
17  import javax.mail.internet.InternetAddress;
18  import javax.mail.internet.MimeMessage;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import com.buckosoft.BSAccount.domain.BSAccount;
24  
25  /**
26   * @author dick
27   *
28   */
29  public class MailSender {
30  	private final static boolean DEBUG = true;
31  	private final Log logger = LogFactory.getLog(getClass());
32  
33  	private	String	smtpServer = "smtp.buckosoft.com";
34  	private	String	fromAddr = "BuckoSoft Account Manager <bsaccount@buckosoft.com>";
35  	private	String	subject = "BuckoSoft Account Validation";
36  	private	String	baseUrl = "http://mae:8087/SPMCards/";
37  	
38  	
39  	/** Set the baseUrl for the validation web page
40  	 * @param baseUrl the baseUrl to set
41  	 */
42  	public void setBaseUrl(String baseUrl) {
43  		this.baseUrl = baseUrl;
44  	}
45  
46  	public void setSmtpHost(String smtpHost) {
47  		this.smtpServer = smtpHost;
48  	}
49  
50  	public String	sendValidation(String appName, BSAccount account) {
51  		if (DEBUG)
52  			logger.info("sendValidation: host=" + this.smtpServer + " to: " + account.getEmail());
53  		StringBuilder sb = new StringBuilder();
54  		sb.append("Hello " + account.getUsername() + ",\r\n\r\n");
55  		sb.append("Someone, hopefully you, has requested this email be sent.\r\n");
56  		sb.append("Follow this link to validate your BuckoSoft account\r\n");
57  		sb.append(baseUrl);
58  		sb.append("/validateUser?");
59  		sb.append("a=" + appName);
60  		sb.append("&t=" + account.getRegisterToken());
61  		sb.append("\r\n\r\nGood Luck!\r\n");
62  		sb.append("The BuckoSoft Account Manager\r\n");
63  		String body = sb.toString();
64  		try {
65  			Properties props = System.getProperties();
66  			// -- Attaching to default Session, or we could start a new one --
67  			props.put("mail.smtp.host", smtpServer);
68  			Session session = Session.getDefaultInstance(props, null);
69  			// -- Create a new message --
70  			Message msg = new MimeMessage(session);
71  			// -- Set the FROM and TO fields --
72  			msg.setFrom(new InternetAddress(fromAddr));
73  			msg.setRecipients(Message.RecipientType.TO,
74  					InternetAddress.parse(account.getEmail(), false));
75  			// -- We could include CC recipients too --
76  			// if (cc != null)
77  			// msg.setRecipients(Message.RecipientType.CC
78  			// ,InternetAddress.parse(cc, false));
79  			// -- Set the subject and body text --
80  			msg.setSubject(subject);
81  			msg.setText(body);
82  			// -- Set some other header information --
83  			msg.setHeader("X-Mailer", "BSAccount mailer");
84  			msg.setSentDate(new Date());
85  			// -- Send the message --
86  			Transport.send(msg);
87  			System.out.println("Message sent OK.");
88  		} catch (Exception ex) {
89  			return(ex.getLocalizedMessage());
90  		}
91  		return("OK");
92  	}
93  }