【问题标题】:Reply to an email automatic Java J2EE ( javamail)自动回复电子邮件 Java J2EE (javamail)
【发布时间】:2020-05-25 23:33:24
【问题描述】:

公共类发送电子邮件{

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

【问题讨论】:

  • 嗨!寻求代码帮助的问题必须包括在问题本身中重现它所需的最短代码,最好是在堆栈片段中。请阅读How to ask 部分以改进您的问题

标签: java angular spring-boot java-ee-8


【解决方案1】:

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

【讨论】:

    【解决方案2】:
        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 直到他确认帐户创建。创建用户时,在用户表中使用boolean isEnable 创建一个额外的列。默认情况下,除非明确设为真,否则这将是假的,即(在单击确认 url 的情况下)提供要在邮件中调用的 url,当单击邮件时,它将打开注册成功的页面并设置 isEnable=true在数据库中。现在,每次用户注册时,它都会在数据库中创建其条目,其中 isEnable 为 false。直到每次用户登录时对其进行检查。您可以在 Spring Security 中轻松做到这一点。

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

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2012-11-22
      • 1970-01-01
      相关资源
      最近更新 更多