Java Email Send sendMail(String vmName, String property, String emailId, boolean usageHigh)

Here you can find the source of sendMail(String vmName, String property, String emailId, boolean usageHigh)

Description

send Mail

License

Apache License

Declaration

public static void sendMail(String vmName, String property, String emailId, boolean usageHigh) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    final static String username = "cmpe283team12";
    final static String password = "12!@qwQW12";

    public static void sendMail(String vmName, String property, String emailId, boolean usageHigh) {
        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");

        String subjectHighUsage = vmName + ": has reached limit " + property + " usage";
        String subjectNormalUsage = vmName + ": You are back to normal " + property + " usage";

        String emailBodyHighUsage = "Hello," + "\n\n THIS IS AN AUTOGENRATED MAIL. PLEASE DO NOT REPLY TO THIS.\n"
                + " This mail is sent as you exceeded Limit for" + property + " on your virtual machine " + vmName
                + "."
                + " To check our usage and monitor the usage please login to the Private Cloud to see the performace of your virtual machine."
                + "\n\n Regards, \n Private Cloud Performance Monitoring Team, \n Private Cloud - TEAM12.";

        String emailBodyNormalUsage = "Hello," + "\n\n THIS IS AN AUTOGENRATED MAIL. PLEASE DO NOT REPLY TO THIS.\n"
                + "\n\n This mail is sent as your are back to normal usage now " + property
                + " on your virtual machine " + vmName
                + " You may login to the Private Cloud to see the performace of your virtual machine." + vmName
                + "\n\n Regards, \n Private Cloud Performance Monitoring Team, \n Private Cloud - TEAM12.";

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }/*from  w  ww  .  ja  va2  s.co  m*/
        });
        session.setDebug(true);
        try {
            if (isValidEmailAddress(emailId)) {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("cmpe283team12@gmail.com"));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailId));
                if (usageHigh) {
                    message.setSubject(subjectHighUsage);
                    message.setText(emailBodyHighUsage);
                } else {
                    message.setSubject(subjectNormalUsage);
                    message.setText(emailBodyNormalUsage);
                }

                Transport.send(message);
                System.out.println("Email sent successfully to " + emailId);
            } else {
                System.out.println(emailId + " is not a valid email address");
            }
        } catch (MessagingException e) {
            System.out.println("Messaging Exception");
            e.printStackTrace();
        }
    }

    private static boolean isValidEmailAddress(String emailId) {
        boolean result = true;
        try {
            InternetAddress emailAddr = new InternetAddress(emailId);
            emailAddr.validate();
        } catch (AddressException ex) {
            result = false;
        }
        return result;
    }
}

Related

  1. sendMail(String filePathName, InternetAddress[] addresses, List> md5cellRows, String today, byte[] report1bytes, byte[] report2bytes, byte[] report3bytes)
  2. sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from)
  3. sendMail(String smtpServer, String to, String from, String subject, String body)
  4. sendMail(String to, String from, String subject, String body, boolean bodyIsHTML)
  5. sendMail(String toEmailId, String subject, String msgText, String from, String smtpServer, String userName, String password)
  6. sendMessage(Message message)
  7. sendMessage(Message msg, Transport transport)
  8. sendMimeMessage(MimeMessage mimeMessage)
  9. sendResetPasswordMail(String to, String message)