Send mail using GMAIL : Email « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Network Protocol » EmailScreenshots 
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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  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.gmail;

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 = "smtp.gmail.com";
  private static final String SMTP_PORT = "465";
  private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  
  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 com.sun.net.ssl.internal.ssl.Provider());
    boolean debug = true;

    Properties props = new Properties();
    props.put("mail.smtp.host", 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() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                    }
    });

    session.setDebug(debug);
    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

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

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/html");
    Transport.send(msg);
  }

  /**
   * 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.