Thursday, March 3, 2011

Using gmail as a SMTP server

SMTP Server: smtp.gmail.com
Use Authentication: Yes
Use Secure Connection: Yes (this can be TLS or SSL depending on your mail client)
Username: your GMail account
Password: your GMail password
Port: 465 or 587

(see http://kb.siteground.com/article/How_to_use_Googles_free_SMTP_server.html from which I have copied this info)


Remember to add activation.jar and mail.jar to your CP.

If you get
java.net.UnknownHostException: smtp.gmail.com

try 66.249.93.109 instead

if you are behind a Proxy; you are screwed
http://www.oracle.com/technetwork/java/faq-135477.html#proxy

see also here for more on JavaMail API

and here for a working example.

Create an account in gmail, say "hellopippo@gmail.com" with password "hello"

This code should definitely work - unless you are behind a proxy:

package com.pierre.email;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
 * A simple email sender class.
 */
public class SimpleSender
{
    /**
     * Main method to send a message given on the command line.
     */
    public static void main(String args[])
    {
       
        try
        {
            String to="vernetto@yahoo.com";
            String from="hellopippo@gmail.com";
            String subject="fanculo";
            String body="sei scemissimo";
            send(to, from, subject, body);
        }
        catch (Exception ex)
        {
            System.out.println("Usage: java com.pierre.email.SimpleSender"
                    + "toAddress fromAddress subjectText bodyText");
        }
        System.exit(0);
    }

    /**
     * "send" method to send the message.
     */
    public static void send(String to, String from
            , String subject, String body)
    {
     String smtpServer = "smtp.gmail.com"; // or 74.125.91.109
        String username = "hellopippo@gmail.com";
        String password = "hello";
        try
        {
            Properties props = System.getProperties();
            // -- Attaching to default Session, or we could start a new one --
           
            props.put("mail.smtp.host", smtpServer); 
            props.put("mail.smtp.port", "587"); // 587 465
     
   props.put("mail.smtp.user", username);
   props.put("mail.smtp.password", password);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");           
           
            Session session = Session.getDefaultInstance(props, null);
            // -- Create a new message --
            Message message = new MimeMessage(session);
           
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
           
            message.setSubject(subject);
            message.setText(body);
            // -- Set some other header information --
            message.setHeader("X-Mailer", "ThisIsPierre");
            message.setSentDate(new Date());
            // -- Send the message --
       
            Transport transport = session.getTransport("smtp");
            transport.connect(smtpServer, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
       
            System.out.println("Message sent OK.");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}



Download JavaMail here

No comments: