Tuesday, August 5, 2014

Email sending code in java

Here full java codes are below for sending email it is most and creative program for java programmer. If you still problem to write code for that see all code listed here.
java is platform independent programming language and it is more secure as comparison to other language, due to this reason many big e-commerce website are build in java.
For using email sending code open net beans or eclipse and go in servlet and write code step by step and run this program.

                                                                   
                                                                   
                                                                   
                                           
package com.sonu.email;

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;

import java.util.*;


public class GmailSender {

    private static String HOST = "smtp.gmail.com";
    private static String USER = "sonu.stoic@gmail.com";
    private static String PASSWORD = "9760769897";
    private static String PORT = "465";
    private static String FROM = "sonu.stoic@gmail.com";
    private static String TO = "anuj.patel58@gmail.com";

    private static String STARTTLS = "true";
    private static String AUTH = "true";
    private static String DEBUG = "true";
    private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static String SUBJECT = " Email Sending Practise";
    private static String TEXT = "Hello Friedz....";
    public static synchronized void send() {
        //Use Properties object to set environment properties
        Properties props = new Properties();

        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.user", USER);

        props.put("mail.smtp.auth", AUTH);
        props.put("mail.smtp.starttls.enable", STARTTLS);
        props.put("mail.smtp.debug", DEBUG);

        props.put("mail.smtp.socketFactory.port", PORT);
        props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        try {

         
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);

         
            MimeMessage message = new MimeMessage(session);
            message.setText(TEXT);
            message.setSubject(SUBJECT);
            message.setFrom(new InternetAddress(FROM));
            message.addRecipient(RecipientType.TO, new InternetAddress(TO));
            message.saveChanges();

       
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, USER, PASSWORD);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
       GmailSender.send();
       System.out.println("Mail sent successfully!");
    }
}

No comments:

Post a Comment

Tricks and Tips