Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / runner / helper / SimpleMailSender.java
blob221de9a750279fe61369eb8246f3ce1b10686919
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package helper;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
31 import java.io.InputStreamReader;
32 import java.io.OutputStreamWriter;
34 import java.net.InetAddress;
35 import java.net.Socket;
36 import java.net.UnknownHostException;
39 public class SimpleMailSender {
40 private String m_MailServer = "unknown";
41 private String m_Sender = "unknown";
42 private String m_Recipient = "unknown";
43 private String m_Subject = "unknown";
44 private String m_Message = "unknown";
47 public String getMailServer() {
48 return m_MailServer;
51 public void setMailServer(String server) {
52 m_MailServer = server;
55 public String getSender() {
56 return m_Sender;
59 public void setSender(String sender) {
60 m_Sender = sender;
63 public String getRecipient() {
64 return m_Recipient;
67 public void setRecipient(String recipient) {
68 m_Recipient = recipient;
71 public String getSubject() {
72 return m_Subject;
75 public void setSubject(String subject) {
76 m_Subject = subject;
79 public String getMessage() {
80 return m_Message;
83 public void setMessage(String msg) {
84 m_Message = msg;
87 public void sendMail() {
88 if (m_MailServer.equals ("unknown")) {
89 System.out.println("No Mailserver given ... exiting");
90 return;
92 if (m_Recipient.equals ("unknown")) {
93 System.out.println("No Recipient given ... exiting");
94 return;
96 sendMail(m_MailServer,m_Sender,m_Recipient,m_Subject,m_Message);
99 public void sendMail(String server, String sender,
100 String recipient, String subject, String msg) {
101 //setting member variables for reuse
102 m_MailServer = server;
103 m_Sender = sender;
104 m_Recipient = recipient;
105 m_Subject = subject;
106 m_Message = msg;
108 try {
109 Socket socket = new Socket(m_MailServer, 25);
110 BufferedReader input =
111 new BufferedReader(new InputStreamReader(
112 socket.getInputStream(), "8859_1"));
113 BufferedWriter output =
114 new BufferedWriter(new OutputStreamWriter(
115 socket.getOutputStream(), "8859_1"));
117 sendline(input, output, "HELO " + getHostName());
118 sendline(input, output, "MAIL FROM: " + m_Sender);
119 sendline(input, output, "RCPT TO: <" + m_Recipient + ">");
120 sendline(input, output, "DATA");
121 sendline(output, "MIME-Version: 1.0");
122 sendline(output, "Subject: " + m_Subject);
123 sendline(output, "From: " + m_Sender);
124 sendline(output, "To: " + m_Recipient);
125 sendline(output,
126 "Content-Type: text/html; charset=\"us-ascii\"\r\n");
128 // Send the body
129 sendline(output, m_Message);
131 sendline(input, output, ".");
132 sendline(input, output, "QUIT");
133 socket.close();
134 } catch (Exception e) {
135 e.printStackTrace();
139 private void sendline(BufferedReader input,
140 BufferedWriter output, String line) {
141 try {
142 output.write(line + "\r\n");
143 output.flush();
144 line = input.readLine();
145 } catch (Exception e) {
146 e.printStackTrace();
150 private void sendline(BufferedWriter output, String line) {
151 try {
152 output.write(line + "\r\n");
153 output.flush();
154 } catch (Exception e) {
155 e.printStackTrace();
159 private String getHostName() {
160 String hostname = "";
162 try {
163 InetAddress addr = InetAddress.getLocalHost();
165 hostname = addr.getHostName();
166 } catch (UnknownHostException e) {
169 return hostname;