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

Reply to an email automatic Java J2EE ( javamail)

public class 发送电子邮件 {

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.I 开发一个 Web 应用程序,想在注册后添加一个自动电子邮件发送功能,但我不知道。如果有人能帮助我,我将不胜感激

此 link 向您展示如何在 j2ee 中发送电子邮件:https://www.codejava.net/java-ee/jsp/sending-e-mail-with-jsp-servlet-and-javamail。所以如果你想在注册后验证账户,只需要生成一个代码发送到那个邮箱并将这个代码保存在会话中,然后将它与用户在验证页面中输入的内容进行比较。希望你能解决。

    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

现在只要用户注册就会调用这个函数。一定要让用户 disable 直到他确认创建帐户。创建用户时,在用户 table 和 boolean isEnable 中创建一个额外的列。默认情况下,这将是错误的,除非明确设为真,即(在单击确认 url 的情况下)在邮件中调用 url,当单击邮件时,它将打开页面注册成功并在数据库中设置isEnable=true。现在,每次用户注册时,它都会在数据库中创建其条目,并将 isEnable 设置为 false。直到每次用户登录时对其进行检查。您可以在 spring 安全性中轻松做到这一点。

在您的邮件中发送了一个唯一 ID (UUID) link与 link 一起编辑(例如 localhost:8080/confirmRegisteration?token=djdkfjksdfjksdhfj 这将转到 confirmRegisteration 端点),用户将点击该 ID .将此 UUID 保存在您的数据库中并在确认时保存。检查UUID是否在link中。如果您指定了有效时间段。然后也检查一下。之后只需将 属性 isEnable 设置为 true 并从数据库中删除 UUID 令牌。