我正在尝试使用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();}
%>
答案 0 :(得分:2)
这些是我经常使用的属性
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
需要设置{}}对于VM等
答案 1 :(得分:0)
尝试使用属性 mail.smtp_port
而不是 mail.smtp.port
或:
mail.smtp.port=
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false