qq邮箱发邮件

 
   static String server = "smtp.qq.com";//邮件服务器
  
      static String from = "张三";//发送者,显示的发件人名字
 
    static String user = "6xxxxxxx1@qq.com";//发送者邮箱地址
 
    static String password = "";//密码

public static void send3(String email, String subject, String body) throws Exception {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host",server);
            props.put("mail.smtp.port","587");
            props.put("mail.smtp.auth","true"); 
            Transport transport = null;
            Session session = Session.getDefaultInstance(props, null);
            transport = session.getTransport("smtp");
            transport.connect(server, user, password);
            MimeMessage msg = new MimeMessage(session);
            msg.setSentDate(new Date());
            InternetAddress fromAddress = new InternetAddress(user,from,"UTF-8");
            msg.setFrom(fromAddress);
            InternetAddress[] toAddress = new InternetAddress[1];
            toAddress[0] = new InternetAddress(email);
            msg.setRecipients(Message.RecipientType.TO, toAddress);
            msg.setSubject(subject, "UTF-8");   
            msg.setText(body, "UTF-8");
            msg.saveChanges();
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注意:1、要在qq邮箱的设置里开通smtp的服务。
2、 密码要填写开通smtp服务时的授权码。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值