restructuring into packages.
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / notification / MailServer.java
1 /**
2  * Copyright (c) 2005 UPS_SCS NL
3  *  
4  */
5 package org.wamblee.crawler.kiss.notification;
6
7 import java.util.Properties;
8
9 import javax.mail.Session;
10
11 import org.apache.commons.mail.Email;
12 import org.apache.commons.mail.EmailException;
13
14 /**
15  * Mail server.
16  */
17 public class MailServer {
18     
19     private String _host; 
20     private int _port;
21     private String _username;
22     private String _password;
23
24     /**
25      * Constructs the mail server interface. 
26      * @param aHost Host name of the SMTP server.
27      * @param aPort Port name of the SMTP server.
28      * @param aUsername Username to use for authentication or null if no authentication is 
29      *   required. 
30      * @param aPassword Password to use for authentication or null if no authenticatio is 
31      *   required. 
32      */
33     public MailServer(String aHost, int aPort, String aUsername, String aPassword) {
34         _host = aHost;
35         _port = aPort;
36         _username = aUsername;
37         _password = aPassword;
38     }
39     
40     /**
41      * Sends an e-mail. 
42      * @param aMail Mail to send. 
43      * @throws EmailException In case of problems sending the mail. 
44      */
45     public void send(Email aMail) throws EmailException {
46         Properties props = new Properties();
47         props.put("mail.transport.protocol", "smtp");
48         props.put("mail.smtp.host", _host);
49         props.put("mail.smtp.port", "" + _port);
50
51         Session mailSession = Session.getInstance(props, new UsernamePasswordAuthenticator(_username, _password));
52         aMail.setMailSession(mailSession);
53         aMail.send();
54     }
55 }