(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / notification / MailServer.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.crawler.kiss.notification;
17
18 import java.util.Properties;
19
20 import javax.mail.Session;
21
22 import org.apache.commons.mail.Email;
23 import org.apache.commons.mail.EmailException;
24
25 /**
26  * Mail server.
27  */
28 public class MailServer {
29
30     private String _host;
31
32     private int _port;
33
34     private String _username;
35
36     private String _password;
37
38     /**
39      * Constructs the mail server.
40      * 
41      * @param aHost
42      *            Host name of the SMTP server.
43      * @param aPort
44      *            Port name of the SMTP server.
45      * @param aUsername
46      *            Username to use for authentication or null if no
47      *            authentication is required.
48      * @param aPassword
49      *            Password to use for authentication or null if no authenticatio
50      *            is required.
51      */
52     public MailServer(String aHost, int aPort, String aUsername,
53             String aPassword) {
54         _host = aHost;
55         _port = aPort;
56         _username = aUsername;
57         _password = aPassword;
58     }
59
60     /**
61      * Sends an e-mail.
62      * 
63      * @param aMail
64      *            Mail to send.
65      * @throws EmailException
66      *             In case of problems sending the mail.
67      */
68     public void send(Email aMail) throws EmailException {
69         Properties props = new Properties();
70         props.put("mail.transport.protocol", "smtp");
71         props.put("mail.smtp.host", _host);
72         props.put("mail.smtp.port", "" + _port);
73
74         Session mailSession = Session.getInstance(props,
75                 new UsernamePasswordAuthenticator(_username, _password));
76         aMail.setMailSession(mailSession);
77         aMail.send();
78     }
79 }