163发送邮件

public static String name = "xxx@163.com";
public static String pwd = "xxx";//授权码  在163里申请
public static String host = "smtp.163.com";
public static String portData = "25";

 这里操作完成申请授权码

public static void mailProperties(String to, String subject, String msgBody) {
        Properties properties = System.getProperties();    //获取系统属性,主要用于设置邮件相关的参数。
        properties.setProperty("mail.smtp.host", host);//发件人邮箱服务地址
        properties.setProperty("mail.smtp.port", portData);//ssl端口
        properties.setProperty("mail.smtp.auth", "true");//用户验证并返回Session,开启用户验证,设置发送邮箱的账号密码。
        properties.setProperty("mail.transport.protocol", "smtp");//使用协议
        properties.setProperty("mail.smtp.ssl.enable", "true");//使用协议

        MailAuthenticator mailAuthenticator = new MailAuthenticator(name, pwd);
        Session session1 = Session.getInstance(properties, mailAuthenticator);   //1、创建session
        session1.setDebug(true);
        session1.setDebugOut(System.out);

        try {
            MimeMessage message = new MimeMessage(session1);  //4、创建邮件
            message.setFrom(new InternetAddress(name)); //发件人

            try {
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, "你好,收到一封邮件", "UTF-8"));//收件人//MimeMessage.RecipientType.TO:发送、CC:抄送BCC:密送

            } catch (Exception e) {
                e.printStackTrace();
            }

            message.setSubject(subject, "UTF-8");
            message.setContent(msgBody, "text/html;charset=UTF-8");
//            Transport transport = session1.getTransport(); //2、通过session得到transport对象
            Transport transport = session1.getTransport("smtp");
//            transport.connect(name,pwd);
            transport.connect();//3、使用邮箱的用户名和密码连上邮件服务器
            transport.sendMessage(message, message.getAllRecipients());//5、发送邮件
            transport.close();
            System.out.println("发送成功!");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


    public static class MailAuthenticator extends Authenticator {
        private String strUser;
        private String strPwd;

        public MailAuthenticator() {
            super();
        }

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            String username = this.strUser;
            String password = this.strPwd;
            if ((username != null) && (username.length() > 0) && (password != null) && (password.length() > 0)) {
                return new PasswordAuthentication(username, password);
            }
            return null;
        }

        public MailAuthenticator(String user, String password) {
            this.strUser = user;
            this.strPwd = password;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值