Friday 5 October 2012

SendingMail using Java

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class GmailSendEmailSSL {
    public static final String USERNAME = "xxxxxxxxxxxxxxxx";
    public static final String PASSWORD = "xxxxxxxxxxxxxxxx";

    public static void main(String[] args) throws Exception {
        //
        // Email information such as from, to, subject and contents.
        //
        String mailFrom = "xxxxxxxxxxxxxxxxxxxxxx";
        String mailTo = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String mailSubject = "TestReport";
        String mailText = "Please check enclosed TestReport";

        GmailSendEmailSSL gmail = new GmailSendEmailSSL();
        gmail.sendMail(mailFrom, mailTo, mailSubject, mailText);
    }

    private void sendMail(String mailFrom, String mailTo,
                          String mailSubject, String mailText)
            throws Exception {

        Properties config = createConfiguration();

        //
        // Creates a mail session. We need to supply username and
        // password for Gmail authentication.
        //
        Session session = Session.getInstance(config, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        GmailSendEmailSSL.USERNAME,
                        GmailSendEmailSSL.PASSWORD
                );
            }
        });

        //
        // Creates email message
        //
        Message message = new MimeMessage(session);
        message.setSentDate(new Date());
        message.setFrom(new InternetAddress(mailFrom));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        message.setSubject(mailSubject);
        message.setText(mailText);

        //
        // Send a message
        //
        Transport.send(message);
    }

    private Properties createConfiguration() {
        return new Properties() {{
            put("mail.smtp.host", "smtp.gmail.com");
            put("mail.smtp.auth", "true");
            put("mail.smtp.port", "465");
            put("mail.smtp.socketFactory.port", "465");
            put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }};
    }
}

7 comments:

  1. It’s really amazing that we can record what our visitors do on our site. Thanks for sharing this awesome guide. I’m happy that I came across with your site this article is on point,thanks again and have a great day. Keep update more information..

    CRO Agency in Chennai

    ReplyDelete
  2. Informative post.I got more knowledge from this blog.Thanks for sharing.... Warehouse Audit
    Stock Audit
    Customer Reconciliation

    ReplyDelete
  3. I really enjoyed your blog Thanks for sharing such an informative post.Looking For Some More Stuff.

    shuttering works

    ReplyDelete