1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SimpleMailSender.java,v $
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 ************************************************************************/
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() {
54 public void setMailServer(String server
) {
55 m_MailServer
= server
;
58 public String
getSender() {
62 public void setSender(String sender
) {
66 public String
getRecipient() {
70 public void setRecipient(String recipient
) {
71 m_Recipient
= recipient
;
74 public String
getSubject() {
78 public void setSubject(String subject
) {
82 public String
getMessage() {
86 public void setMessage(String msg
) {
90 public void sendMail() {
91 if (m_MailServer
.equals ("unknown")) {
92 System
.out
.println("No Mailserver given ... exiting");
95 if (m_Recipient
.equals ("unknown")) {
96 System
.out
.println("No Recipient given ... exiting");
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
;
107 m_Recipient
= recipient
;
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
);
129 "Content-Type: text/html; charset=\"us-ascii\"\r\n");
132 sendline(output
, m_Message
);
134 sendline(input
, output
, ".");
135 sendline(input
, output
, "QUIT");
137 } catch (Exception e
) {
142 private void sendline(BufferedReader input
,
143 BufferedWriter output
, String line
) {
145 output
.write(line
+ "\r\n");
147 line
= input
.readLine();
148 } catch (Exception e
) {
153 private void sendline(BufferedWriter output
, String line
) {
155 output
.write(line
+ "\r\n");
157 } catch (Exception e
) {
162 private String
getHostName() {
163 String hostname
= "";
166 InetAddress addr
= InetAddress
.getLocalHost();
168 hostname
= addr
.getHostName();
169 } catch (UnknownHostException e
) {