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