-2

public class SendEmail {

public static void main(String[] args) {
    //authentication info
    final String username = "mailing@gmail.com";
    final String password = "pass";
    String fromEmail = "mailing@gmail.com";
    String toEmail = "mailing@yahoo.com";

    Properties properties = new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");

    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username,password);
        }
    });
    //Start our mail message
    MimeMessage msg = new MimeMessage(session);
    try {
        msg.setFrom(new InternetAddress(fromEmail));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        msg.setSubject("Subject Line");

        Multipart emailContent = new MimeMultipart();

        //Text body part
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText("My multipart text");

        //Attachment body part.
        MimeBodyPart pdfAttachment = new MimeBodyPart();
        pdfAttachment.attachFile("/Users/hmidi/Downloads/BigData.pdf");

        //Attach body parts
        emailContent.addBodyPart(textBodyPart);
        emailContent.addBodyPart(pdfAttachment);

        //Attach multipart to message
        msg.setContent(emailContent);

        Transport.send(msg);
        System.out.println("Sent message");
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

} `I am trying to develop a web application with JavaEE.I would like to add an automatic email sending functionality as after registration but I have no idea. if anyone could help me, i would be very grateful.

  • 1
    Hi! Questions seeking code help must include the shortest code necessary to reproduce it in the question itself preferably in a Stack Snippet. Please, read the [How to ask](https://stackoverflow.com/help/how-to-ask) section to improve your question – Inazense Feb 10 '20 at 16:06

2 Answers2

0

This link show you how to send email in j2ee: https://www.codejava.net/java-ee/jsp/sending-e-mail-with-jsp-servlet-and-javamail. So if you want to verify account after registration, just need to generate a code to send to that email and save this code in session, and then compare it to what user input in verify page. Hope you will solve it.

TIenHT
  • 61
  • 1
  • 7
0
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    mailMessage.setTo(userTable.getEmail());
    mailMessage.setSubject("Complete Registration!");
    mailMessage.setFrom("v**********@gmail.com");
    mailMessage.setText("To confirm your account, please click here : "
            );
    return mailMessage;
    javaMailSender.sendEmail(mailMessage);

In your application.properties file

spring.mail.host=smtp.gmail.com
spring.mail.password=*************
spring.mail.username=v*************@gmail.com
spring.mail.port=587

Now as soon as user Register call this function. Be sure to make user disable till he confirms the account creation. when creating the user make a extra column in the user table with boolean isEnable. By default this will be false unless explicitly made true i.e (in event of clicking the confirmation url) Give the url to be called in the mail and when clicked on the mail it will open the page with the Registeration Successful and set the isEnable=true in the database. Now every time the user will sign up it will create its entry in database with isEnable as false. Till then perform a check on it everytime when userlogin. You can do this easily in spring security.

In your mail sent a unique id (UUID) linked with the link (e.g. localhost:8080/confirmRegisteration?token=djdkfjksdfjksdhfj This will go to confirmRegisteration endpoint ) that is to be clicked by the user. Save this UUID in your database and at the time of confirmation. Check whether the UUID is in the link or not. If you have specified a valid time period. then check that too. After that just set the property isEnable to be true and delete the UUID token from the database.

Silverfang
  • 349
  • 1
  • 8