Saturday, April 25, 2020

JavaMailApi

  1.  SEND A MAIL THROUGH GMAIL

Downloads the javax.mail.jar    ( JavaMail API )

package demo;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class mailsend {
public static void main(String[] args)
{
 final String username = "suman123@gmail.com";

    final String password = "********";

    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");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("suman1ew@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("suman.giri007@gmail.com"));
        message.setSubject("Automation report");
        message.setText("PFA");

        MimeBodyPart messageBodyPart = new MimeBodyPart();
       MimeBodyPart messageBodyPart1 = new MimeBodyPart();
        Multipart multipart = new MimeMultipart();

       messageBodyPart = new MimeBodyPart();
      messageBodyPart1 = new MimeBodyPart();
        ///https://myaccount.google.com/lesssecureapps
        String file = "D:/sumanFramework/suman/Report/jenkinReport/report.html";
        String fileName = "Report";
        DataSource source = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(fileName);
        messageBodyPart1.setText("-----------------------------------");
        multipart.addBodyPart(messageBodyPart);
       multipart.addBodyPart(messageBodyPart1);
        message.setContent(multipart);

        System.out.println("Sending");

        Transport.send(message);

        System.out.println("Done");

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

Setting to gmail before sending the mail

Log on gmail account, in Account ->
click Security -> turn off 2-step verification and turn on "Less secure app access"


    1.  SEND A MAIL THROUGH OUTLOOK 

     package demo;

    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    public class outlook {

    public static void main(String[] args)
    {
     final String username = "suman@abc.com";

        final String password = "*****";

        Properties props = new Properties();
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.host", "outlook.office365.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("suman@abc.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("suman@abc.com"));
            message.setSubject("Automation report");
            message.setText("PFA");

            MimeBodyPart messageBodyPart = new MimeBodyPart();
            MimeBodyPart messageBodyPart1 = new MimeBodyPart();
            Multipart multipart = new MimeMultipart();

            //messageBodyPart = new MimeBodyPart();
            
            ///https://myaccount.google.com/lesssecureapps
            String file = "D:/suman Framework/Mphasis/suman/jenkinReport/report.html";
            String fileName = "Report";
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
          
            multipart.addBodyPart(messageBodyPart);
            multipart.addBodyPart(messageBodyPart1);
            message.setContent(multipart);

            System.out.println("Sending");

            Transport.send(message);

            System.out.println("Done");

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

    if getting an authentication error then check antivirus setting or outlook setting ( in case of auth for office 360)

    https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_other-mso_o365b/smtpoffice365com-535-573-authentication/2e708a73-3340-4d24-b4e5-b97ff999dbae