Birthday auto mail wish java code

Here is an example of Java code that sends an automated birthday wish email to a list of contacts:

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class BirthdayAutoMail {
    public static void main(String[] args) {
        // List of contacts with their birthdays
        List<Contact> contacts = new ArrayList<>();
        contacts.add(new Contact("John Doe", "[email protected]", "1990-02-28"));
        contacts.add(new Contact("Jane Smith", "[email protected]", "1992-03-15"));
        contacts.add(new Contact("Bob Johnson", "[email protected]", "1985-06-12"));

        // Set up email properties
        Properties props = new Properties();
        props.put("mail.smtp.host", "your-smtp-host");
        props.put("mail.smtp.port", "your-smtp-port");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        // Create a session
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("your-email-username", "your-email-password");
            }
        });

        // Loop through contacts and send birthday wishes
        for (Contact contact : contacts) {
            if (isBirthday(contact)) {
                sendBirthdayEmail(contact, session);
            }
        }
    }

    private static boolean isBirthday(Contact contact) {
        // Check if today's date matches the contact's birthday
        String[] birthdayParts = contact.getBirthday().split("-");
        int month = Integer.parseInt(birthdayParts[1]);
        int day = Integer.parseInt(birthdayParts[2]);
        int todayMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        int todayDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        return month == todayMonth && day == todayDay;
    }

    private static void sendBirthdayEmail(Contact contact, Session session) {
        try {
            // Create a message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("your-email-username", "Your Name"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(contact.getEmail()));
            message.setSubject("Happy Birthday!");
            message.setText("Dear " + contact.getName() + ",\n\nHappy birthday! Wishing you a wonderful day and a great year ahead.\n\nBest regards, Your Name");

            // Send the message
            Transport.send(message);
            System.out.println("Birthday email sent to " + contact.getEmail());
        } catch (MessagingException e) {
            System.out.println("Error sending birthday email to " + contact.getEmail() + ": " + e.getMessage());
        }
    }

    private static class Contact {
        private String name;
        private String email;
        private String birthday;

        public Contact(String name, String email, String birthday) {
            this.name = name;
            this.email = email;
            this.birthday = birthday;
        }

        public String getName() {
            return name;
        }

        public String getEmail() {
            return email;
        }

        public String getBirthday() {
            return birthday;
        }
    }
}

This code uses the JavaMail API to send an email to each contact on their birthday. The isBirthday method checks if today's date matches the contact's birthday, and the sendBirthdayEmail method sends the email using the JavaMail API.

You'll need to replace the placeholders (your-smtp-host, your-smtp-port, your-email-username, and your-email-password) with your actual email server settings and credentials.

Note that this code assumes that the contacts' birthdays are stored in the format YYYY-MM-DD. You may need to adjust the isBirthday method to match your specific birthday format.