• JavaEmail邮箱


    package service;

    import com.sun.mail.util.MailSSLSocketFactory;
    import javax.mail.Message;
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    import java.util.Properties;
    public class EmailBox {

    //根据需要输入参数,我这里用到两个
    public String mail(String a , String b) {

    // JDK9以上或JDK6以下使用mail.jar包不加JAF的activation.jar包会抛出该错误!JDK6以上不需要加该jar包;
    String email = a;
    String num = b;
    //随机数拼成验证码
    // for (int i = 0; i < 6; i++) {
    // int n = (int) (Math.random() * 10);
    // num = num + Integer.toString(n);
    // }
    Properties prop = new Properties();
    //// 开启debug调试,以便在控制台查看
    // prop.setProperty("mail.debug", "true");
    // 设置邮件服务器主机名,这里用QQ邮箱
    prop.setProperty("mail.host", "smtp.qq.com");
    // 发送服务器需要身份验证
    prop.setProperty("mail.smtp.auth", "true");
    // 发送邮件协议名称
    prop.setProperty("mail.transport.protocol", "smtp");

    try {
    // 开启SSL加密,否则会失败
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    prop.put("mail.smtp.ssl.enable", "true");
    prop.put("mail.smtp.ssl.socketFactory", sf);
    // 创建session
    Session session = Session.getInstance(prop);
    // 通过session得到transport对象
    Transport ts = session.getTransport();
    // 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
    ts.connect("smtp.qq.com", "这里填写QQ帐号", "这里填写QQ安全登陆码");//后面的字符是授权码,用qq密码反正我是失败了(用自己的,别用我的,这个号是我瞎编的,为了。。。。)
    // 创建邮件
    MimeMessage message = createSimpleMail(session, email, num);
    // 发送邮件
    ts.sendMessage(message, message.getAllRecipients());
    ts.close();
    return "发送成功!";
    } catch (Exception e) {
    e.printStackTrace();
    return "发送失败!";
    }
    }

    public static MimeMessage createSimpleMail(Session session, String email, String num)
    throws Exception {
    // 创建邮件对象
    MimeMessage message = new MimeMessage(session);
    // 指明邮件的发件人
    message.setFrom(new InternetAddress("这里填写我的QQ邮箱,例如xxxxxxxxx@qq.com"));
    // 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("这里填写你要发送的邮箱,如xxxxxxx@qq.com"));
    // 邮件的标题
    message.setSubject("这里填写要发送的邮件标题");
    // 邮件的文本内容
    message.setContent("这里填写要发送的邮件内容" , "text/html;charset=UTF-8");
    // 返回创建好的邮件对象
    return message;
    }
    }
  • 相关阅读:
    C# 访问AD查询用户信息
    js UTC时间转本地时间
    Silverlight中的序列化和反序列化
    ASP.NET FORM认证配置排错记录
    opencv中cvSetData用法
    WS2812B-64位 8*8位 RGB LED点阵
    1602 LCDKeypad Shield
    Wemos D1 使用ESP8266 板载存储
    Wemos D1 ESP8266的网络工作模式
    Wemos D1 1602 液晶屏幕
  • 原文地址:https://www.cnblogs.com/c2g5201314/p/10502126.html
Copyright © 2020-2023  润新知