import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class EmailSender { public static void main(String[] args) { // Condition for sending email boolean conditionToSendEmail = true; // Modify this based on your condition if (conditionToSendEmail) { String to = "receiver@example.com"; String from = "sender@example.com"; String host = "smtp.example.com"; String password = "password"; // Your email password // Setup mail server properties Properties properties = System.getProperties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", "587"); // You can use 465 for SSL properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // Get the default Session object Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // Create a default MimeMessage object MimeMessage message = new MimeMessage(session); // Set From: header field message.setFrom(new InternetAddress(from)); // Set To: header field message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Set the actual message message.setText("This is the body of the email!"); // Send message Transport.send(message); System.out.println("Email Sent Successfully!"); } catch (MessagingException mex) { mex.printStackTrace(); } } else { System.out.println("Condition not met. Email not sent."); } } }