我正在尝试使用 javamail 将电子邮件从一个帐户发送到另一个帐户。但代码无法执行 Transport.send(msg);线。可能的原因是什么?
下面是下面的jsp代码。
<%
String host = "localhost";
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
try {
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.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
} catch (MessagingException mex) {mex.printStackTrace();}
%>
这些是我经常使用的属性
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", host);
properties.put("mail.user", from);
properties.put("mail.smtp.port", smtpPort);
properties.put("mail.smtp.localhost", "myHost");
Session session = Session.getInstance(properties, null);
你会注意到我设置了mail.smtp.localhost
如果机器主机名未正确设置,则需要设置该值,例如对于虚拟机等
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)