Package KFM

Source Code of KFM.Mail

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


//  Mail

// ************ package ******************************************************
package KFM;

// ************ imports ******************************************************

// KFM packages
import KFM.log.*;

// java packages
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import javax.activation.DataHandler;

/** <H2> Mail utility.</H2>
*
*
*/
public class Mail
{

    // ************************************************************
    // Constants
    // ************************************************************

    /** CodeSet */
    private static String mCodeSet = "ISO-8859-1";


    // ************************************************************
    // Variables
    // ************************************************************

    // ************************************************************
    // Methods
    // ************************************************************

    /** This message sends an email with the following parameters:
     *
     * @param aRecipients   recipients
     * @param aCCs          cc
     * @param aBccs         bcc
     * @param aSubject      subject
     * @param aMessage      message
     * @param aFrom         from
     * @param aMailHost     mailhost
     *
     * Currently only plain text emails are supported.
     */

    public static boolean sendMail(String[] aRecipients,
                                   String[] aCCs,
                                   String[] aBccs,
                                   String   aSubject,
                                   String   aMessage,
                                   boolean  aIsHtml,
                                   String   aFrom,
                                   String   aMailHost) {
        try{
            boolean tDebug = false;

            //Set the host smtp address
            Properties tProps = new Properties();
            tProps.put("mail.smtp.host",  aMailHost);

            // Patch P02B18
            // Try to repair exception:
            //     javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25
            tProps.put("mail.host", aMailHost);
            tProps.put("mail.transport.protocol", "smtp");
            // End Patch P02B18

            // create some properties and get the default Session
            Session tSession = Session.getDefaultInstance(tProps, null);
            tSession.setDebug(tDebug);

            // create a message
            MimeMessage tMsg = new MimeMessage(tSession);

            // set the from address
            InternetAddress tAddressFrom = new InternetAddress(aFrom);
            tMsg.setFrom(tAddressFrom);

            // set the to address
            InternetAddress[] tAddressTo = new InternetAddress[aRecipients.length];
            for (int i = 0; i < aRecipients.length; i++)
            {
                tAddressTo[i] = new InternetAddress(aRecipients[i]);
            }
            tMsg.setRecipients(Message.RecipientType.TO, tAddressTo);

            // set cc address
            InternetAddress[] tAddressCC = new InternetAddress[aCCs.length];
            for (int i = 0; i < aCCs.length; i++)
            {
                tAddressCC[i] = new InternetAddress(aCCs[i]);
            }
            tMsg.setRecipients(Message.RecipientType.CC, tAddressCC);

            // set bcc address
            InternetAddress[] tAddressBCC = new InternetAddress[aBccs.length];
            for (int i = 0; i < aBccs.length; i++)
            {
                tAddressBCC[i] = new InternetAddress(aBccs[i]);
            }
            tMsg.setRecipients(Message.RecipientType.BCC, tAddressBCC);

            // Set the Subject
            tMsg.setSubject(aSubject, mCodeSet);

            // Set the message Body in a dirrerent way (HTML or plain text)
            if (aIsHtml) {
                DataHandler tDh = new DataHandler(aMessage, "text/html");
                tMsg.setDataHandler(tDh);
            } else {
                tMsg.setText(aMessage, mCodeSet);
            }

            Transport.send(tMsg);

        } catch (MessagingException me){
            KFMSystem.log.error("KFM.Mail", "Mailhost is '" + aMailHost + "'", me);
            return false;
        }
        return true;
    }


    public static void main(String[] args)
    {
        String tFromTo = args[0];
        if (tFromTo == null) {
            System.out.println("Please give one eMail (From=To) address as parameter.");
            System.exit(-1);
        }

        String tBody = "<html><body><h1>Hallo Bernd</h1>Das ist HTML</body></html>";

        boolean tSendSuccessful = Mail.sendMail(
                new String[]{tFromTo}, //To
                new String[]{}, //CC
                new String[]{}, //BCC
                "KFM.Mail test", //Subject
                tBody, //Body
                true, //isHTML
                tFromTo, //From
                "karoshi.mchp.siemens.de");

        System.out.println(tSendSuccessful ? "Mail sent successfully :-)" : "Mail not sent :-(");
    }

}
TOP

Related Classes of KFM.Mail

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.