因此,我最近遵循了有关如何通过smtp发送电子邮件的教程。它正在工作,但是我想使用来自数据库的信息来修改消息。我从将对象从控制器传递到jsp来访问我的信息。
样品
<c:forEach items="${applicationInfo }" var="application">
${application.name}
</c:forEach>
这是我的电子邮件代码。
<%
String host = "smtp.gmail.com";
String user="****@gmail.com";//Your E-mail-Id
String pass="*****"; //your e-mail account password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = "******@gmail.com"; //recipient E-mail-Id
String from = "********@gmail.com"; // Your E-mail Id
String subject ="test mail";
String messageText = "<h3>Test Info</h3>";
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");//port number 465 for Secure (SSL) and we can also use port no 587 for Secure (TLS)
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html");
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
try {
transport.sendMessage(msg, msg.getAllRecipients());
out.println("Send Success");
boolean WasEmailSent = true; // assume it was sent
}
catch (Exception err) {
boolean WasEmailSent = false;
}
transport.close();
%>
这是我第一次使用它,我不确定。请帮我。如果您可以提出更好的方法,请发表评论。非常感谢。 :**