Send Mail Implementation using simple SMTP : Email « Network Protocol « Java

Send Mail Implementation using simple SMTP


 * Copyright 2010 Commerce4J.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.

//package com.commerce4j.storefront.utils.smtp;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

 * SendMail Implementation using simple SMTP.
 * @author carlos.quijano
public class SendMailImpl {

    private String smtpHost = "localhost";

    public void sendMessage(String from, String[] recipients, String subject, String message)
    throws MessagingException {
        boolean debug = false;

         //Set the host smtp address
         Properties props = new Properties();
         props.put("", smtpHost);

            // create some properties and get the default Session
            Session session = Session.getDefaultInstance(props, null);

            // create a message
            Message msg = new MimeMessage(session);

            // set the from and to address
            InternetAddress addressFrom = new InternetAddress(from);

            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int i = 0; i < recipients.length; i++) {
                addressTo[i] = new InternetAddress(recipients[i]);
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            // Setting the Subject and Content Type
            msg.setContent(message, "text/html");

    public String getSmtpHost() {
        return smtpHost;

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;



Related examples in the same category

1.Pure Java Email client
2.Sending Mail Using Sockets
3.Sending Mail
4.Get Email Message Example
5.A Client to Send SMTP MailA Client to Send SMTP Mail
6.Mailer: Sends an email message
7.TestOpenMailRelay -- send self-returning SPAM to check for relay sitesTestOpenMailRelay -- send self-returning SPAM to check for relay sites
8.Sender -- send an email message
9.Sender -- send an email message with attachment
10.SendMime -- send a multi-part MIME email message
11.Read a file return mail headers one at a time
12.Send mail using GMAIL
13.Validator for Zip code, Email, Phone number
14.Send email out