Daredevil
Daredevil

Reputation: 1802

java- How to use services for sending email?

I have a simple web application where different users can log into it. One of the important feature is user can access a document and send email of it's content to an outsider like third party. Below is just how the email looks like to give an idea:

enter image description here

It's pretty self explanatory and I can send to multiple user if I want like [email protected],[email protected],... in the field box shown.With all this, I am using Java Mail API to make it work and after hitting the send button,it sends directly to the recipient.No issue at all.

Now, I want to modify this by doing this email feature as a service.What this means is when I send the email,the content and info filled in will be stored in a table in MYSQL and the service(running in background) will pick up from the table and do the sending.

This is my function:

public void sendEmail(String recipient, String subject, String content,
                      String host, String port, final String senderaddress, 
                      final String password) {
    try {
        System.out.println("Please Wait, sending email...");

        /*Setup mail server */
        Properties props = new Properties();
        props.put("mail.smtp.host", host); //SMTP Host
        props.put("mail.smtp.port", port); //TLS Port
        props.put("mail.smtp.auth", "true"); //enable authentication
        props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
        //create Authenticator object to pass in Session.getInstance argument
        Authenticator auth = new Authenticator() {
            //override the getPasswordAuthentication method
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(senderaddress, password);
            }
        };
        Session session = Session.getInstance(props, auth);
        session.setDebug(true);

        // Define message
        MimeMessage message = new MimeMessage(session);
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(senderaddress));
        message.addRecipients(Message.RecipientType.TO,
                              InternetAddress.parse(recipient));
        // Set Subject: header field
        message.setSubject(subject);

        // Now set the actual message
        message.setText(content);
        try {
            Transport.send(message);
        } catch (AddressException addressException) {
            addressException.printStackTrace();
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

Can this be done in the way I want because I am unsure how to make it work?

Upvotes: 2

Views: 581

Answers (1)

Ajinkya
Ajinkya

Reputation: 345

1 ) After hitting Sending mail button from UI, You need to call a method for saving data like recipient, subject, content in DB

2)Write an email sender Service which retrieves non_delivered / pending mail from DB table and send it through Java Mail API

3)Scheduled email sender service with the help of ScheduledExecutorService

Upvotes: 2

Related Questions