[英]Java Mail Error : java.net.ConnectException: Connection refused: connect
[英]java mail give java.net.connectexception: connection refused
我在Windows上有相同的代碼,並且效果很好。 當我將代碼移至centos時,它給出了異常: javax.mail.MessagingException:無法連接到SMTP主機:stmp.gmail.com,端口:587; 嵌套的異常是:java.net.ConnectException:連接被拒絕
有人可以給我一些關於此異常的建議嗎?
非常感謝你。
您拼寫錯誤! 它應該是“ smtp .gmail.com”而不是“ stmp .gmail.com”。
“拒絕連接”是兩件事之一。 您指定的host:port不正確,或者介入的防火牆沒有發揮作用。
如果您只是在學習如何通過Java發送郵件,請嘗試以下操作,否則,您需要將其設置為電子郵件提供商的SMTP服務器,然后該SMTP服務器將郵件發送到適當的位置,而情況並非如此此代碼。
注意:該代碼是用Java Servlet編寫的。
public class MailClient extends HttpServlet
{
private class SMTPAuthenticator extends Authenticator
{
private PasswordAuthentication authentication;
public SMTPAuthenticator(String login, String password)
{
authentication = new PasswordAuthentication(login, password);
}
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
String from = "xyz.com";
String to = "abc.com";
String subject = "Your Subject.";
String message = "Message Text.";
String login = "xyz.com";
String password = "password";
Properties props = new Properties();
props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator(login, password);
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
try
{
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
Transport.send(msg);
}
catch (MessagingException ex)
{
Logger.getLogger(MailClient.class.getName()).
log(Level.SEVERE, null, ex);
}
}
finally
{
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}
@Override
public String getServletInfo()
{
return "Short description";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.