Java 从glassfish服务器发送邮件时出错
我有一个玻璃鱼服务器,我正试图通过该服务器发送邮件。但是有一个错误 代码如下Java 从glassfish服务器发送邮件时出错,java,email,glassfish,Java,Email,Glassfish,我有一个玻璃鱼服务器,我正试图通过该服务器发送邮件。但是有一个错误 代码如下 import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.MessagingException;
public class EmailService{
public static void main(String[] args) {
String to="to@gmail.com";//change accordingly
String from="from@";//change accordingly
String password="password";//change accordingly
System.out.println("started");
Properties props = new Properties();
props.put("mail.smtps.host", "localhost");
props.put("mail.smtps.socketFactory.port", "8080");
props.put("mail.smtps.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtps.auth", "true");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.connectiontimeout", "90000");
props.put("mail.smtps.timeout", "90000");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
System.out.println("started");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress("to"));
message.setSubject("Nothing Special..");
message.setText("Send Mail By Java Programmm....");
System.out.println("started");
//send message
System.out.println(message);
Transport.send(message);
System.out.println("started");
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}
但是我犯了一个错误。我已更改端口详细信息。创建的Javamail会话也是glassfishserver
javax.mail.internet.MimeMessage@6daa8eb7
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingExcep
tion: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
我想您没有在
localhost
上安装邮件服务器?如果我尝试连接到SMTP.company.com,我也会收到相同的错误。如果我是从office网络连接,请检查您的邮件客户端设置。您可以使用代码中的内容