1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 package helper; 28 29 import java.io.BufferedReader; 30 import java.io.BufferedWriter; 31 import java.io.InputStreamReader; 32 import java.io.OutputStreamWriter; 33 34 import java.net.InetAddress; 35 import java.net.Socket; 36 import java.net.UnknownHostException; 37 38 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"; 45 46 47 public String getMailServer() { 48 return m_MailServer; 49 } 50 51 public void setMailServer(String server) { 52 m_MailServer = server; 53 } 54 55 public String getSender() { 56 return m_Sender; 57 } 58 59 public void setSender(String sender) { 60 m_Sender = sender; 61 } 62 63 public String getRecipient() { 64 return m_Recipient; 65 } 66 67 public void setRecipient(String recipient) { 68 m_Recipient = recipient; 69 } 70 71 public String getSubject() { 72 return m_Subject; 73 } 74 75 public void setSubject(String subject) { 76 m_Subject = subject; 77 } 78 79 public String getMessage() { 80 return m_Message; 81 } 82 83 public void setMessage(String msg) { 84 m_Message = msg; 85 } 86 87 public void sendMail() { 88 if (m_MailServer.equals ("unknown")) { 89 System.out.println("No Mailserver given ... exiting"); 90 return; 91 } 92 if (m_Recipient.equals ("unknown")) { 93 System.out.println("No Recipient given ... exiting"); 94 return; 95 } 96 sendMail(m_MailServer,m_Sender,m_Recipient,m_Subject,m_Message); 97 } 98 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; 107 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")); 116 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"); 127 128 // Send the body 129 sendline(output, m_Message); 130 131 sendline(input, output, "."); 132 sendline(input, output, "QUIT"); 133 socket.close(); 134 } catch (Exception e) { 135 e.printStackTrace(); 136 } 137 } 138 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(); 147 } 148 } 149 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(); 156 } 157 } 158 159 private String getHostName() { 160 String hostname = ""; 161 162 try { 163 InetAddress addr = InetAddress.getLocalHost(); 164 165 hostname = addr.getHostName(); 166 } catch (UnknownHostException e) { 167 } 168 169 return hostname; 170 } 171 } 172