本文主要是介绍Java 发邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 收件人电子邮箱//String to = "fupeng@okjiaoyu.cn";// 发件人电子邮箱String from = "fupeng@okjiaoyu.cn";// 指定发送邮件的主机为 localhostString host = "smtp.exmail.qq.com";// 获取系统属性Properties properties = System.getProperties();// 设置邮件服务器properties.setProperty("mail.smtp.host", host);properties.put("mail.smtp.auth", "true");// 获取默认session对象Session session = Session.getDefaultInstance(properties,new Authenticator(){public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication("fupeng@okjiaoyu.cn", "kunpengkU1"); //发件人邮件用户名、密码}});try{// 创建默认的 MimeMessage 对象MimeMessage message = new MimeMessage(session);// Set From: 头部头字段message.setFrom(new InternetAddress(from));String [] to = {"fupeng@okjiaoyu.cn","fupeng_2005@126.com"};InternetAddress[] sendTo = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { System.out.println("发送到:" + to[i]); sendTo[i] = new InternetAddress(to[i]); } // Set To: 头部头字段message.setRecipients(Message.RecipientType.TO,sendTo);// Set Subject: 头部头字段message.setSubject("This is the Subject Line!");// 设置消息体message.setText("This is actual message");// 发送消息Transport.send(message);System.out.println("Sent message successfully....from w3cschool.cc");}catch (MessagingException mex) {mex.printStackTrace();}
这篇关于Java 发邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!