2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import java
.io
.BufferedReader
;
21 import java
.io
.BufferedWriter
;
22 import java
.io
.InputStreamReader
;
23 import java
.io
.OutputStreamWriter
;
25 import java
.net
.InetAddress
;
26 import java
.net
.Socket
;
27 import java
.net
.UnknownHostException
;
30 public class SimpleMailSender
{
31 private String m_MailServer
= "unknown";
32 private String m_Sender
= "unknown";
33 private String m_Recipient
= "unknown";
34 private String m_Subject
= "unknown";
35 private String m_Message
= "unknown";
38 public String
getMailServer() {
42 public void setMailServer(String server
) {
43 m_MailServer
= server
;
46 public String
getSender() {
50 public void setSender(String sender
) {
54 public String
getRecipient() {
58 public void setRecipient(String recipient
) {
59 m_Recipient
= recipient
;
62 public String
getSubject() {
66 public void setSubject(String subject
) {
70 public String
getMessage() {
74 public void setMessage(String msg
) {
78 public void sendMail() {
79 if (m_MailServer
.equals ("unknown")) {
80 System
.out
.println("No Mailserver given ... exiting");
83 if (m_Recipient
.equals ("unknown")) {
84 System
.out
.println("No Recipient given ... exiting");
87 sendMail(m_MailServer
,m_Sender
,m_Recipient
,m_Subject
,m_Message
);
90 public void sendMail(String server
, String sender
,
91 String recipient
, String subject
, String msg
) {
92 //setting member variables for reuse
93 m_MailServer
= server
;
95 m_Recipient
= recipient
;
100 Socket socket
= new Socket(m_MailServer
, 25);
101 BufferedReader input
=
102 new BufferedReader(new InputStreamReader(
103 socket
.getInputStream(), "8859_1"));
104 BufferedWriter output
=
105 new BufferedWriter(new OutputStreamWriter(
106 socket
.getOutputStream(), "8859_1"));
108 sendline(input
, output
, "HELO " + getHostName());
109 sendline(input
, output
, "MAIL FROM: " + m_Sender
);
110 sendline(input
, output
, "RCPT TO: <" + m_Recipient
+ ">");
111 sendline(input
, output
, "DATA");
112 sendline(output
, "MIME-Version: 1.0");
113 sendline(output
, "Subject: " + m_Subject
);
114 sendline(output
, "From: " + m_Sender
);
115 sendline(output
, "To: " + m_Recipient
);
117 "Content-Type: text/html; charset=\"us-ascii\"\r\n");
120 sendline(output
, m_Message
);
122 sendline(input
, output
, ".");
123 sendline(input
, output
, "QUIT");
125 } catch (Exception e
) {
130 private void sendline(BufferedReader input
,
131 BufferedWriter output
, String line
) {
133 output
.write(line
+ "\r\n");
135 line
= input
.readLine();
136 } catch (Exception e
) {
141 private void sendline(BufferedWriter output
, String line
) {
143 output
.write(line
+ "\r\n");
145 } catch (Exception e
) {
150 private String
getHostName() {
151 String hostname
= "";
154 InetAddress addr
= InetAddress
.getLocalHost();
156 hostname
= addr
.getHostName();
157 } catch (UnknownHostException e
) {