发送一封简单的邮件

     

MimeMessage message = null; final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketFactory.fallback", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.user", user); props.put("mail.password", pw); Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication(){ String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; Session mailSession = Session.getInstance(props, authenticator); message = new MimeMessage(mailSession); message.setFrom(new InternetAddress("邮箱地址", "邮箱名称", "UTF-8")); //1、是否需要回执--非必须 message.setHeader("Disposition-Notification-To", "<gyli1992@qq.com>"); //收件人地址 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("gyoyinli1992@qq.com", "user_to", "UTF-8")); //主题 message.setSubject("test02"); //内容 message.setContent("<a href='taoquan.tlong.top' style='color:#ccc'>淘券网</a>", "text/html;charset=UTF-8"); //显示发送时间 message.setSentDate(new Date()); //保存更改 message.saveChanges(); //输出eml文件 OutputStream out = new FileOutputStream("D://myemail.eml"); message.writeTo(out); out.flush(); out.close(); //发送邮件 Transport.send(message); System.out.println("邮件发送成功");
原文地址:https://www.cnblogs.com/guoyinli/p/8303485.html