Send mail using GMAIL : Email « Network Protocol « Java

Send mail using GMAIL

 * 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.

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

 * Sendmail using GMAIL implementation.
 * @author carlos.quijano
 * @version $Revision$ $Date$
public class SendMailImpl {
  private static final String SMTP_HOST_NAME = "";
  private static final String SMTP_PORT = "465";
  private static final String SSL_FACTORY = "";
  private String username;
  private String password;

  /* (non-Javadoc)
   * @see com.commerce4j.storefront.utils.SendMail#sendMessage(java.lang.String, java.lang.String[], java.lang.String, java.lang.String)
  public void sendMessage(
      String from, String recipients[], 
      String subject, String message 
  ) throws MessagingException {
    // Security.addProvider(new;
    boolean debug = true;

    Properties props = new Properties();
    props.put("", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);

    Message msg = new MimeMessage(session);
    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");

   * JavaBean Getter, Gets the username current value.
   * @return The username current value.
  public String getUsername() {
    return username;

   * JavaBean Setter, Sets value to username.
   * @param username The value of username to set.
  public void setUsername(String username) {
    this.username = username;

   * JavaBean Getter, Gets the password current value.
   * @return The password current value.
  public String getPassword() {
    return password;

   * JavaBean Setter, Sets value to password.
   * @param password The value of password to set.
  public void setPassword(String password) {
    this.password = password;




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 Implementation using simple SMTP
13.Validator for Zip code, Email, Phone number
14.Send email out