Tuesday, April 2, 2013

Send Email through Java via Gmail,Yahoo,Hotmail or your hosting Email server

How to send email from java? .For this we need an JavaMail API .Include this jar file in your class path by placing it into YourProject\WebContent\WEB-INF\lib or if you are not using EE version then add it into environment variable.
        
     First we need to login to mail account.
  • Email server consist two types of port, one is for incoming mail(POP, IMAP) and another is for outgoing mail(SMTP).For sending purpose we only need SMTP port of that particular Email server.
  • Second thing is how to send authentication data to server using insecure network.To maintain security there is two protocol SSL and TLS which send authentication data(Email,Password) to server in encrypted form,If we use TLS ,it allow both secure and unsecure connection,and if SSL it only allow secure Connection.That is up to user to choose the protocol
  • In java to carry Email account setting,we use Properties,These Email settings are 
            1. smtp host
            2. smtp port (either for protocol SSL or for protocol TLS)
            3  valid EmailId and password of active account
     Now Code for Login in Gmail using SSL protocol
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com"); / /      Host Name
        props.put("mail.smtp.socketFactory.port", "465");/ /       SSL Port     
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
            { protected PasswordAuthentication getPasswordAuthentication()
                {  return new PasswordAuthentication("YOUREMAIL@GMAIL.COM", "PASSWORD");
                 }
             });
     
 Now Code for Login in Gmail using TLS protocol
       Actually Gmail use STARTTLS

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");/ /       Host Name
        props.put("mail.smtp.port", "587"); / /    TLS Port
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
            { protected PasswordAuthentication getPasswordAuthentication()
                {  return new PasswordAuthentication("YOUREMAIL@GMAIL.COM", "PASSWORD");
                 }
             });
  • Now after login we have to compose email,First understand our email message cannot simply transfer through http,for this a particular standard are set and those are mime standard,our email server use mime to transfer email ,first it convert the email message into mime format and transfer it on network Know more about Mime
  • Email Header contain all the information about the message and Routing information.
  • Use MimeMessage to create mail format. It provide option to set From ,where and To whom ,email is going to be sent and it also provide setSubject to set the Subject of Email.
  •  After that,it give freedom to setContext of email.Use text/html if you want to send html in message else there is no need to setContext to text/html
  • At Last Transport.send(message),same as we click on send button in email client application
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("YOUREMAIL@GMAIL.COM"));
      message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(TO@XYZ.COM));
      message.setSubject("My First Email using Behind Java Scene");
      String msg="Thank's to Behind Java Scene to teach the core aspect of Email sending "
      message.setContent(msg, "text/html; charset=utf-8");
      Transport.send(message);
  
   Email Server Settings for Yahoo
     Only change the host Name and SMTP port from Login code 
     Host Name :smtp.mail.yahoo.com
     SMTP port: 465 
        Properties props = new Properties();
        props.put("mail.smtp.socketFactory.port", "465");  
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");
        props.put("mail.smtp.auth", true);  
   Email Server Settings for Hotmail 
     Only change the host Name and SMTP port from Login code in SSL section 
     Host Name: smtp.live.com 
     SMTP port: 25  
 Same for Hosting Server only change your particular Host Name and change your specific SMTP port

15 comments :

Post a Comment