发送一封简单的邮件

     

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", ""); //收件人地址 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("[email protected]", "user_to", "UTF-8")); //主题 message.setSubject("test02"); //内容 message.setContent("淘券网", "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("邮件发送成功");

 

你可能感兴趣的:(发送一封简单的邮件)