package org.example.demo.uebung3.aufgabe13;

import jakarta.annotation.Resource;
import jakarta.ejb.Stateless;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

@Stateless
public class Mailsender implements MailsenderLocal {

    private static final Logger LOG = Logger.getLogger(Mailsender.class.getName());

    @Resource(mappedName="java:/jboss/mail/uebung3")
    private Session mailSession;

    public Mailsender() {
    }

    public void sendMail(String recipient, String subject, String text) {
        try {
            jakarta.mail.Message message = new MimeMessage(mailSession);
            message.setSentDate(new Date());
            message.addRecipient(jakarta.mail.Message.RecipientType.TO, new InternetAddress(recipient));
            message.setSubject(subject);
            message.setText(text);
            Transport.send(message);
        }
        catch (MessagingException e) {
            LOG.log(Level.SEVERE, "Fehler beim Senden der E-Mail an " + recipient, e);
        }
    }
}
