回复电子邮件自动Java J2EE(javamail)

回复电子邮件自动Java J2EE(javamail),java,angular,spring-boot,java-ee-8,Java,Angular,Spring Boot,Java Ee 8,公共类发送电子邮件{ 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";

公共类发送电子邮件{

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();
    }

}
}
`我正在尝试用JavaEE开发一个web应用程序。我想在注册后添加一个自动电子邮件发送功能,但我不知道。如果有人能帮助我,我将不胜感激。

此链接演示如何在j2ee中发送电子邮件:。所以,如果您想在注册后验证帐户,只需要生成一个代码发送到该电子邮件,并在会话中保存该代码,然后将其与用户在验证页面中输入的内容进行比较。希望你能解决它

    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);
在application.properties文件中

spring.mail.host=smtp.gmail.com
spring.mail.password=*************
spring.mail.username=v*************@gmail.com
spring.mail.port=587
现在只要用户注册调用这个函数。确保用户
禁用
,直到他确认帐户创建。创建用户时,使用
布尔值isEnable在用户表中创建一个额外的列。默认情况下,这将为false,除非显式设置为true,即(在单击确认url的情况下)提供要在邮件中调用的url,当单击邮件时,它将打开页面,注册成功,并在数据库中设置
isEnable=true
。现在,每次用户注册时,它都会在数据库中创建其条目,isEnable为false。直到每次用户登录时都对其进行检查。您可以在SpringSecurity中轻松实现这一点


在您的邮件中,发送了一个与用户要单击的链接(例如,
localhost:8080/confirmRegistration?token=djdkfjksdfjksdfj
链接的唯一id(UUID)。在确认时将此UUID保存在数据库中。检查UUID是否在链接中。如果指定了有效的时间段。那也检查一下。之后,只需将属性
isEnable
设置为true,并从数据库中删除UUID令牌。

Hi!寻求代码帮助的问题必须包含在问题中重现问题所需的最短代码,最好是在堆栈片段中。请阅读该部分以改进您的问题