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 ************************************************************************/
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() {
51 public void setMailServer(String server
) {
52 m_MailServer
= server
;
55 public String
getSender() {
59 public void setSender(String sender
) {
63 public String
getRecipient() {
67 public void setRecipient(String recipient
) {
68 m_Recipient
= recipient
;
71 public String
getSubject() {
75 public void setSubject(String subject
) {
79 public String
getMessage() {
83 public void setMessage(String msg
) {
87 public void sendMail() {
88 if (m_MailServer
.equals ("unknown")) {
89 System
.out
.println("No Mailserver given ... exiting");
92 if (m_Recipient
.equals ("unknown")) {
93 System
.out
.println("No Recipient given ... exiting");
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
;
104 m_Recipient
= recipient
;
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
);
126 "Content-Type: text/html; charset=\"us-ascii\"\r\n");
129 sendline(output
, m_Message
);
131 sendline(input
, output
, ".");
132 sendline(input
, output
, "QUIT");
134 } catch (Exception e
) {
139 private void sendline(BufferedReader input
,
140 BufferedWriter output
, String line
) {
142 output
.write(line
+ "\r\n");
144 line
= input
.readLine();
145 } catch (Exception e
) {
150 private void sendline(BufferedWriter output
, String line
) {
152 output
.write(line
+ "\r\n");
154 } catch (Exception e
) {
159 private String
getHostName() {
160 String hostname
= "";
163 InetAddress addr
= InetAddress
.getLocalHost();
165 hostname
= addr
.getHostName();
166 } catch (UnknownHostException e
) {