001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.mail; 025 026 import java.net.InetAddress; 027 import java.util.Properties; 028 029 import javax.mail.Message; 030 import javax.mail.Session; 031 import javax.mail.Transport; 032 import javax.mail.internet.InternetAddress; 033 import javax.mail.internet.MimeBodyPart; 034 import javax.mail.internet.MimeMessage; 035 import javax.mail.internet.MimeMultipart; 036 037 import org.ujmp.core.util.ConsoleUtil; 038 039 public class MailUtil { 040 041 public static void sendSystemOut(String recipient, String subject, 042 String smtpServer, String userName, String password) 043 throws Exception { 044 sendMessage(recipient, subject, ConsoleUtil.getSystemOut(), smtpServer, 045 userName, password); 046 } 047 048 public static void sendSystemErr(String recipient, String subject, 049 String smtpServer, String userName, String password) 050 throws Exception { 051 sendMessage(recipient, subject, ConsoleUtil.getSystemErr(), smtpServer, 052 userName, password); 053 } 054 055 public static void sendMessage(String recipient, String subject, 056 String text, String smtpServer, String userName, String password) 057 throws Exception { 058 059 Properties properties = new Properties(); 060 // properties.put("mail.store.protocol", "pop3"); 061 properties.put("mail.transport.protocol", "smtp"); 062 properties.put("mail.user", userName); 063 // properties.put("mail.pop3.host", "pop3.provider.de"); 064 properties.put("mail.smtp.host", smtpServer); 065 // properties.put("User", "user"); 066 if (password != null) { 067 properties.put("Password", password); 068 } 069 properties.put("mail.from", "ujmp@" 070 + InetAddress.getLocalHost().getHostName()); 071 072 Session mailSession = Session.getInstance(properties); 073 074 Message message = new MimeMessage(mailSession); 075 message.setSubject(subject); 076 message.setRecipient(Message.RecipientType.TO, new InternetAddress( 077 recipient)); 078 MimeMultipart mimeMultipart = new MimeMultipart(); 079 MimeBodyPart textPart = new MimeBodyPart(); 080 textPart.setText(text); 081 textPart.setDisposition(MimeBodyPart.INLINE); 082 mimeMultipart.addBodyPart(textPart); 083 message.setContent(mimeMultipart); 084 message.saveChanges(); 085 086 Transport.send(message); 087 } 088 089 }