简体   繁体   中英

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:

在此处输入图片说明

It's pretty self explanatory and I can send to multiple user if I want like abc@example.com,efg@hotmail.com,... 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM