public class SendEmail { public void sendEmailBySmtp(MessageVo emailSentRecord) throws Exception { Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", 587); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); MimeMessage msg = new MimeMessage(session); String smtpUserName = "xxxxxxxx"; // 带有权限的 AWS 帐号 String smtpUserPassword = "xxxxxxxx"; // 带有权限的 AWS 密码 msg.setFrom(new InternetAddress("xxxx@xxxxxxx.xxx")); // 发送的 email 帐号 msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(emailSentRecord.getToMailAddress())); msg.setSubject(emailSentRecord.getSubject()); msg.setContent(emailSentRecord.getContent(), "text/html"); Transport transport = session.getTransport(); try { transport.connect("email-smtp." + "us-east-1" + ".amazonaws.com", smtpUserName, smtpUserPassword); transport.sendMessage(msg, msg.getAllRecipients()); //System.out.println("success post"); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(); } finally { transport.close(); } //System.out.println("enter end"); } } public class MessageVo { private String subject; private String toMailAddress; private String content; public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getToMailAddress() { return toMailAddress; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public void setToMailAddress(String toMailAddress) { this.toMailAddress = toMailAddress; } }
转载自:https://blog.csdn.net/qq_33811662/article/details/80794588
转载时请注明出处及相应链接,本文永久地址:http://blog.meken.net/24642.html


微信打赏

支付宝打赏
感谢您对作者van的打赏,我们会更加努力! 如果您想成为作者,请点我