library names now start with wamblee- to make them unique.
[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
21     private int _port;
22
23     private String _username;
24
25     private String _password;
26
27     /**
28      * Constructs the mail server.
29      * 
30      * @param aHost
31      *            Host name of the SMTP server.
32      * @param aPort
33      *            Port name of the SMTP server.
34      * @param aUsername
35      *            Username to use for authentication or null if no
36      *            authentication is required.
37      * @param aPassword
38      *            Password to use for authentication or null if no authenticatio
39      *            is required.
40      */
41     public MailServer(String aHost, int aPort, String aUsername,
42             String aPassword) {
43         _host = aHost;
44         _port = aPort;
45         _username = aUsername;
46         _password = aPassword;
47     }
48
49     /**
50      * Sends an e-mail.
51      * 
52      * @param aMail
53      *            Mail to send.
54      * @throws EmailException
55      *             In case of problems sending the mail.
56      */
57     public void send(Email aMail) throws EmailException {
58         Properties props = new Properties();
59         props.put("mail.transport.protocol", "smtp");
60         props.put("mail.smtp.host", _host);
61         props.put("mail.smtp.port", "" + _port);
62
63         Session mailSession = Session.getInstance(props,
64                 new UsernamePasswordAuthenticator(_username, _password));
65         aMail.setMailSession(mailSession);
66         aMail.send();
67     }
68 }