运用javamail出现: Could not connect to SMTP host: localhost, port: 25;问题,求解,代码如下

时间:2021-07-18 14:32:57
package sendMail;
import java.util.*;
import javax.mail.*;

import javax.mail.internet.MimeMessage; 
import javax.mail.internet.*;
public class Send_Mail {

public static boolean mail_Text(Mail_SendProper mailSender) {

MailAttorney attorney = null;
if(mailSender.isIsvalidate()) {
attorney= new MailAttorney(mailSender.getUserName(),mailSender.getUserPsw());

}

Session sendMailSession =Session.getInstance(mailSender.getProperties(),attorney);

try {
Message mailMessage = new MimeMessage(sendMailSession);

Address from= new InternetAddress(mailSender.getSendAddr());
mailMessage.setFrom(from);

Address to = new InternetAddress(mailSender.getReceiveAddr());
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(mailSender.getSubject());

mailMessage.setSentDate(new Date());
String mailContent= mailSender.getContent();
mailMessage.setText(mailContent);

Transport.send(mailMessage);

return true ;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;


}


public static boolean Html_Mail(Mail_SendProper mailSender) {

MailAttorney attorney= null;
if(mailSender.isIsvalidate()) {
attorney = new MailAttorney(mailSender.getUserName(),mailSender.getUserPsw()) ;

}
Session sendMailSession = Session.getInstance(mailSender.getProperties(), attorney);
try {
Message mailMessage=new MimeMessage(sendMailSession);
Address from= new InternetAddress(mailSender.getSendAddr());
mailMessage.setFrom(from);

Address to = new InternetAddress(mailSender.getReceiveAddr());
mailMessage.setRecipient(Message.RecipientType.TO,to);
mailMessage.setSubject(mailSender.getSubject());
mailMessage.setSentDate(new Date());
Multipart mainPart= new MimeMultipart();

BodyPart html= new MimeBodyPart();
html.setContent(mailSender.getContent(),"text/html;charset=GBK");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
return  true;


} catch(MessagingException ex) {
ex.printStackTrace();
}
return false ;

}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Mail_SendProper mailSender = new Mail_SendProper();
mailSender.setHost("smtp.sina.com");
mailSender.setPort("25");
mailSender.setIsvalidate(true);
mailSender.setUserName("zhh8812@sina.com");
mailSender.setUserPsw("00000000");
mailSender.setSendAddr("zhh8812@sina.com");
mailSender.setReceiveAddr("414333701@qq.com");
mailSender.setSubject("UTF-8");
mailSender.setContent("你好吗?");

mail_Text(mailSender);
Html_Mail(mailSender);
System.out.print("发送成功");

}

}



package sendMail;

import java.util.Properties;

public class Mail_SendProper {
private String host;
private String port="";
private String sendAddr;
private String receiveAddr;

private String userName;
private String userPsw;
private boolean isvalidate = true;

private String subject;
private String content;
private String[] fileNames;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

public String getSendAddr() {
return sendAddr;
}

public void setSendAddr(String sendAddr) {
this.sendAddr = sendAddr;
}

public String getReceiveAddr() {
return receiveAddr;
}

public void setReceiveAddr(String receiveAddr) {
this.receiveAddr = receiveAddr;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPsw() {
return userPsw;
}

public void setUserPsw(String userPsw) {
this.userPsw = userPsw;
}

public boolean isIsvalidate() {
return isvalidate;
}

public void setIsvalidate(boolean isvalidate) {
this.isvalidate = isvalidate;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String[] getFileNames() {
return fileNames;
}

public void setFileNames(String[] fileNames) {
this.fileNames = fileNames;
}

//获得邮件会话属性
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.stmp.host", this.host);
p.put("mail.smtp.port", this.port );
p.put("mail.smtp.auth",isvalidate?"true":"false");
return p ;

}
}


package sendMail;
import javax.mail.*;

public class MailAttorney extends Authenticator {
private String userName;
private String password;

public MailAttorney(String username,String password){
this.userName=username;
this.password= password;
}

//覆盖父类的该方法,获得密码验证器
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,password);
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}




运行出现javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at sendMail.Send_Mail.mail_Text(Send_Mail.java:33)
at sendMail.Send_Mail.main(Send_Mail.java:94)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 8 more
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at sendMail.Send_Mail.Html_Mail(Send_Mail.java:68)
at sendMail.Send_Mail.main(Send_Mail.java:95)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 8 more
求解!!!!!

1 个解决方案

#1


运行出现javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

访问本机的25端口被拒绝。
1.用命令检查25端口是否已经开启 telnet 127.0.0.1 25
2.关闭掉防火墙,比如windows防火墙、360等。

#1


运行出现javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

访问本机的25端口被拒绝。
1.用命令检查25端口是否已经开启 telnet 127.0.0.1 25
2.关闭掉防火墙,比如windows防火墙、360等。