1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.io.BufferedReader;
26cdf0e10cSrcweir import java.io.BufferedWriter;
27cdf0e10cSrcweir import java.io.InputStreamReader;
28cdf0e10cSrcweir import java.io.OutputStreamWriter;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import java.net.InetAddress;
31cdf0e10cSrcweir import java.net.Socket;
32cdf0e10cSrcweir import java.net.UnknownHostException;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir public class SimpleMailSender {
36cdf0e10cSrcweir     private String m_MailServer = "unknown";
37cdf0e10cSrcweir     private String m_Sender = "unknown";
38cdf0e10cSrcweir     private String m_Recipient = "unknown";
39cdf0e10cSrcweir     private String m_Subject = "unknown";
40cdf0e10cSrcweir     private String m_Message = "unknown";
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
getMailServer()43cdf0e10cSrcweir     public String getMailServer() {
44cdf0e10cSrcweir         return m_MailServer;
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
setMailServer(String server)47cdf0e10cSrcweir     public void setMailServer(String server) {
48cdf0e10cSrcweir         m_MailServer = server;
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
getSender()51cdf0e10cSrcweir     public String getSender() {
52cdf0e10cSrcweir         return m_Sender;
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
setSender(String sender)55cdf0e10cSrcweir     public void setSender(String sender) {
56cdf0e10cSrcweir         m_Sender = sender;
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
getRecipient()59cdf0e10cSrcweir     public String getRecipient() {
60cdf0e10cSrcweir         return m_Recipient;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir 
setRecipient(String recipient)63cdf0e10cSrcweir     public void setRecipient(String recipient) {
64cdf0e10cSrcweir         m_Recipient = recipient;
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
getSubject()67cdf0e10cSrcweir     public String getSubject() {
68cdf0e10cSrcweir         return m_Subject;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
setSubject(String subject)71cdf0e10cSrcweir     public void setSubject(String subject) {
72cdf0e10cSrcweir         m_Subject = subject;
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
getMessage()75cdf0e10cSrcweir     public String getMessage() {
76cdf0e10cSrcweir         return m_Message;
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
setMessage(String msg)79cdf0e10cSrcweir     public void setMessage(String msg) {
80cdf0e10cSrcweir         m_Message = msg;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
sendMail()83cdf0e10cSrcweir     public void sendMail() {
84cdf0e10cSrcweir         if (m_MailServer.equals ("unknown")) {
85cdf0e10cSrcweir             System.out.println("No Mailserver given ... exiting");
86cdf0e10cSrcweir             return;
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         if (m_Recipient.equals ("unknown")) {
89cdf0e10cSrcweir             System.out.println("No Recipient given ... exiting");
90cdf0e10cSrcweir             return;
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir         sendMail(m_MailServer,m_Sender,m_Recipient,m_Subject,m_Message);
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
sendMail(String server, String sender, String recipient, String subject, String msg)95cdf0e10cSrcweir     public void sendMail(String server, String sender,
96cdf0e10cSrcweir         String recipient, String subject, String msg) {
97cdf0e10cSrcweir         //setting member variables for reuse
98cdf0e10cSrcweir         m_MailServer = server;
99cdf0e10cSrcweir         m_Sender = sender;
100cdf0e10cSrcweir         m_Recipient = recipient;
101cdf0e10cSrcweir         m_Subject = subject;
102cdf0e10cSrcweir         m_Message = msg;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         try {
105cdf0e10cSrcweir             Socket socket = new Socket(m_MailServer, 25);
106cdf0e10cSrcweir             BufferedReader input =
107cdf0e10cSrcweir                 new BufferedReader(new InputStreamReader(
108cdf0e10cSrcweir                         socket.getInputStream(), "8859_1"));
109cdf0e10cSrcweir             BufferedWriter output =
110cdf0e10cSrcweir                 new BufferedWriter(new OutputStreamWriter(
111cdf0e10cSrcweir                         socket.getOutputStream(), "8859_1"));
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             sendline(input, output, "HELO " + getHostName());
114cdf0e10cSrcweir             sendline(input, output, "MAIL FROM: " + m_Sender);
115cdf0e10cSrcweir             sendline(input, output, "RCPT TO: <" + m_Recipient + ">");
116cdf0e10cSrcweir             sendline(input, output, "DATA");
117cdf0e10cSrcweir             sendline(output, "MIME-Version: 1.0");
118cdf0e10cSrcweir             sendline(output, "Subject: " + m_Subject);
119cdf0e10cSrcweir             sendline(output, "From: " + m_Sender);
120cdf0e10cSrcweir             sendline(output, "To: " + m_Recipient);
121cdf0e10cSrcweir             sendline(output,
122cdf0e10cSrcweir                 "Content-Type: text/html; charset=\"us-ascii\"\r\n");
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             // Send the body
125cdf0e10cSrcweir             sendline(output, m_Message);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir             sendline(input, output, ".");
128cdf0e10cSrcweir             sendline(input, output, "QUIT");
129cdf0e10cSrcweir             socket.close();
130cdf0e10cSrcweir         } catch (Exception e) {
131cdf0e10cSrcweir             e.printStackTrace();
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
sendline(BufferedReader input, BufferedWriter output, String line)135cdf0e10cSrcweir     private void sendline(BufferedReader input,
136cdf0e10cSrcweir         BufferedWriter output, String line) {
137cdf0e10cSrcweir         try {
138cdf0e10cSrcweir             output.write(line + "\r\n");
139cdf0e10cSrcweir             output.flush();
140cdf0e10cSrcweir             line = input.readLine();
141cdf0e10cSrcweir         } catch (Exception e) {
142cdf0e10cSrcweir             e.printStackTrace();
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
sendline(BufferedWriter output, String line)146cdf0e10cSrcweir     private void sendline(BufferedWriter output, String line) {
147cdf0e10cSrcweir         try {
148cdf0e10cSrcweir             output.write(line + "\r\n");
149cdf0e10cSrcweir             output.flush();
150cdf0e10cSrcweir         } catch (Exception e) {
151cdf0e10cSrcweir             e.printStackTrace();
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
getHostName()155cdf0e10cSrcweir     private String getHostName() {
156cdf0e10cSrcweir         String hostname = "";
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         try {
159cdf0e10cSrcweir             InetAddress addr = InetAddress.getLocalHost();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             hostname = addr.getHostName();
162cdf0e10cSrcweir         } catch (UnknownHostException e) {
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         return hostname;
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir }
168