Gmail验证码发送

服务器 amazon

服务器需要配置出站规则,在安全组将 465 端口号 设置为可出站

账号 gmail 账号 密码即可


public class EmailServiceImpl implements EmailService {

    @Value("${javamail.host}")
    private String  host; //smtp.gmail.com
    @Value("${javamail.port}")
    private Integer port;//465
    @Value("${javamail.username}")
    private String  userName;//账号
    @Value("${javamail.password}")
    private String  password;//密码

    //发送邮件的模板引擎
    @Autowired
    private FreeMarkerConfigurer configurer;

    public MessageTo<Object> sendModelMail(EmailInfoMo info, String code) {

        try {
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost(host);
            mailSender.setPort(port);
            mailSender.setUsername(userName);
            mailSender.setPassword(password);
            Properties mailProp = mailSender.getJavaMailProperties();
            mailProp.put("mail.transport.protocol", "smtp");
            mailProp.put("mail.smtp.auth", "true");
            mailProp.put("mail.smtp.starttls.enable", "true");
            mailProp.put("mail.smtp.starttls.required", "true");        
            mailProp.put("mail.smtp.ssl.enable", "true");
            Template template = configurer.getConfiguration().getTemplate(info.getTempleteName());
            Map model = new HashMap<>();
            model.put("code", code);
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom(userName);
            helper.setTo(info.getTo());
            helper.setSubject(info.getSubject());
            helper.setText(text, true);
            mailSender.send(message);
            return new MessageTo<>(1, MessageToCode.AUTHCODE_SEND_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MessageTo<>(0, MessageToCode.AUTHCODE_SEND_FAIL);
    }
}

若出现连接超时,原因为防火墙,或放在外网服务器进行测试 

要使用 Java 发送邮件验证码,需用到 Java Mail API 库。以下是发送邮件验证码的基本步骤: 1. 确定发件人、收件人、SMTP 服务器地址和端口号; 2. 创建 JavaMail Session 对象,并设置发件人身份验证信息; 3. 使用 JavaMail Session 对象创建一个邮件消息; 4. 设置邮件消息的内容、主题、收发件人等信息; 5. 发送邮件消息。 具体实现代码可以参考以下示例代码: ```java import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendEmail { public static void main(String[] args) { // 发件人信息 String from = "your_email@example.com"; String password = "your_email_password"; // 收件人信息 String to = "recipient_email@example.com"; // SMTP 服务器信息 String host = "smtp.example.com"; int port = 587; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.port", port+""); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.starttls.enable", "true"); // 获取默认会话对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建邮件消息 MimeMessage message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置邮件主题 message.setSubject("验证码"); // 生成随机验证码 int code = (int)(Math.random() * 9000 + 1000); // 设置邮件内容 message.setText("您的验证码是:" + code); // 发送邮件 Transport.send(message); System.out.println("邮件发送。"); } catch (MessagingException e) { System.out.println("发送邮件出现异常:" + e.getMessage()); } } } ``` 以上代码以 Gmail SMTP 服务器为例,需要将 `smtp.example.com` 和端口号 `587` 修改为相应的 SMTP 服务器地址和端口号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值