【java】写一个发送邮箱的样例

06-23 1648阅读

【java】写一个发送邮箱的样例
package com.ai157.aigc.controller.methods;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import java.util.Properties;
public class SendEmail {
    /*    public static void main(String[] args) {
              toMsg("3333");
        }*/
    public static void toMsg(String code) {
        String host = "smtp.qq.com";
        String username = "11111111@qq.com";
        String password = "1111111111";//这一个是要去邮箱服务商那授权,具体咨询服务商。
        String to = "22222222@qq.com";
        String from = "11111111@qq.com";
        String subject = "你有新的邮箱了";
        String context = "新的验证码为:"+code;
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            //如果你的代码在阿里云上,25端口被关闭了,可以换成587
            props.put("mail.smtp.port", "587");
            Session session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(context);
            Transport.send(message);
            System.out.println(new Date()+"邮箱发送完毕!请查收");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}
【java】写一个发送邮箱的样例
(图片来源网络,侵删)
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]