【发布时间】:2014-11-13 14:29:52
【问题描述】:
我正在使用 JAVA 邮件 API 发送邮件并使用 SMTP。当我将代码放入测试服务器时,它正在成功发送邮件,因为它有互联网连接。但在生产中没有互联网连接。是否可以使用 Web 服务器发送邮件?我用来发送邮件的代码如下。
final String username = "momkutty@gmail.com";
final String password = "password";
Properties props = new Properties();
System.out.println("Setting properties");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.google.com");
props.put("mail.smtp.port", "25");
System.out.println("Properties set successfully");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
System.out.println("Setting message properties");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("momkutty@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("sumankalyan.roy@cmcltd.com"));
message.setSubject("Test Subject");
message.setText("Hey!!!! This is a TEST mail using SMTP SMTP port no.25.....");
System.out.println("Going to send mail...");
Transport.send(message);
System.out.println("Mail has been Sent");
} catch (MessagingException e) {
e.printStackTrace();
}
}e
【问题讨论】:
-
我不明白“生产没有互联网连接”。所以你的服务器只为你的局域网服务,对吗?你的局域网中需要一个可以访问互联网的邮件服务器。我不认为这是一个java问题。这更像是一个基础设施问题。而且,没有网络服务器不是邮件服务器。
标签: smtp jakarta-mail