update dev300-m58
[ooovba.git] / qadevOOo / runner / helper / SimpleMailSender.java
blob4b2eac5bb8cd826fb456a6a93131b0ce8236fffc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SimpleMailSender.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package helper;
32 import java.io.BufferedReader;
33 import java.io.BufferedWriter;
34 import java.io.InputStreamReader;
35 import java.io.OutputStreamWriter;
37 import java.net.InetAddress;
38 import java.net.Socket;
39 import java.net.UnknownHostException;
42 public class SimpleMailSender {
43 private String m_MailServer = "unknown";
44 private String m_Sender = "unknown";
45 private String m_Recipient = "unknown";
46 private String m_Subject = "unknown";
47 private String m_Message = "unknown";
50 public String getMailServer() {
51 return m_MailServer;
54 public void setMailServer(String server) {
55 m_MailServer = server;
58 public String getSender() {
59 return m_Sender;
62 public void setSender(String sender) {
63 m_Sender = sender;
66 public String getRecipient() {
67 return m_Recipient;
70 public void setRecipient(String recipient) {
71 m_Recipient = recipient;
74 public String getSubject() {
75 return m_Subject;
78 public void setSubject(String subject) {
79 m_Subject = subject;
82 public String getMessage() {
83 return m_Message;
86 public void setMessage(String msg) {
87 m_Message = msg;
90 public void sendMail() {
91 if (m_MailServer.equals ("unknown")) {
92 System.out.println("No Mailserver given ... exiting");
93 return;
95 if (m_Recipient.equals ("unknown")) {
96 System.out.println("No Recipient given ... exiting");
97 return;
99 sendMail(m_MailServer,m_Sender,m_Recipient,m_Subject,m_Message);
102 public void sendMail(String server, String sender,
103 String recipient, String subject, String msg) {
104 //setting member variables for reuse
105 m_MailServer = server;
106 m_Sender = sender;
107 m_Recipient = recipient;
108 m_Subject = subject;
109 m_Message = msg;
111 try {
112 Socket socket = new Socket(m_MailServer, 25);
113 BufferedReader input =
114 new BufferedReader(new InputStreamReader(
115 socket.getInputStream(), "8859_1"));
116 BufferedWriter output =
117 new BufferedWriter(new OutputStreamWriter(
118 socket.getOutputStream(), "8859_1"));
120 sendline(input, output, "HELO " + getHostName());
121 sendline(input, output, "MAIL FROM: " + m_Sender);
122 sendline(input, output, "RCPT TO: <" + m_Recipient + ">");
123 sendline(input, output, "DATA");
124 sendline(output, "MIME-Version: 1.0");
125 sendline(output, "Subject: " + m_Subject);
126 sendline(output, "From: " + m_Sender);
127 sendline(output, "To: " + m_Recipient);
128 sendline(output,
129 "Content-Type: text/html; charset=\"us-ascii\"\r\n");
131 // Send the body
132 sendline(output, m_Message);
134 sendline(input, output, ".");
135 sendline(input, output, "QUIT");
136 socket.close();
137 } catch (Exception e) {
138 e.printStackTrace();
142 private void sendline(BufferedReader input,
143 BufferedWriter output, String line) {
144 try {
145 output.write(line + "\r\n");
146 output.flush();
147 line = input.readLine();
148 } catch (Exception e) {
149 e.printStackTrace();
153 private void sendline(BufferedWriter output, String line) {
154 try {
155 output.write(line + "\r\n");
156 output.flush();
157 } catch (Exception e) {
158 e.printStackTrace();
162 private String getHostName() {
163 String hostname = "";
165 try {
166 InetAddress addr = InetAddress.getLocalHost();
168 hostname = addr.getHostName();
169 } catch (UnknownHostException e) {
172 return hostname;