Saturday, July 12, 2014

Sending Email Using Java




download Java Mail Download



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


public class SendMail {

    private String HOST_NAME = "gmail-smtp.l.google.com";
    String messageBody;

    public void postMail(String recipients, String subject, String message,String yourEmail, String yourEmailPassword) throws MessagingException{

        boolean debug = false;
        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", HOST_NAME);
        props.put("mail.smtp.auth", "true");

        Authenticator authenticator = new SMTPAuthenticator(yourEmail, yourEmailPassword);
        Session session = Session.getDefaultInstance(props, authenticator);

        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(yourEmail);
        msg.setFrom(addressFrom);


        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(message);

        Multipart multipart = new MimeMultipart();

        //add the message body to the mime message
        multipart.addBodyPart(messageBodyPart);

        //Put all message parts in the message
        msg.setContent(multipart);
        Transport.send(msg);
        System.out.println("Sucessfully Sent mail to All Users");
     
    }

   

    class SMTPAuthenticator extends javax.mail.Authenticator {

        String username;
        String password;

        private SMTPAuthenticator(String authenticationUser, String authenticationPassword) {
            username = authenticationUser;
            password = authenticationPassword;
        }

        public PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(username, password);
        }
    }

}




class SendTestEmail{
   
    public static void main(String[] args) {
        try {
            String recipients = "resiver@gmail.com";
            String yourEmail = "myEmail@gmail.com";
            String yourPassword = "myPassword";
            String subject = "Test";
            String message = "Hi, this is my mail";
           
           
            SendMail sendMail = new SendMail();
            sendMail.postMail(recipients, subject, message, yourEmail, yourPassword);
        } catch (MessagingException ex) {
            Logger.getLogger(SendTestEmail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
















No comments:

Post a Comment