发送邮件错误,javax.mail.MessaginException:无法连接到SMTP主机:localhost,端口:25;
这是我的密码发送邮件错误,javax.mail.MessaginException:无法连接到SMTP主机:localhost,端口:25;,java,netbeans,jakarta-mail,sendmail,connectionexception,Java,Netbeans,Jakarta Mail,Sendmail,Connectionexception,这是我的密码 import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSendClass {
public static void main (String [] args){
// Recipient's email ID needs to be mentioned.
String to = "abc82@gmail.com";
// Sender's email ID needs to be mentioned
String from = "xyz@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Thanks for registering on our website!");
// Now set the actual message
message.setText("Welcome To Job Portal !!!! Again Thanks ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
每次我都会犯这个错误
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:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at MailSendClass.main(MailSendClass.java:58)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more
BUILD SUCCESSFUL (total time: 3 seconds)
我不明白为什么会发生这种情况。请帮助我修复此错误。您应该查看以下两行:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
Caused by: java.net.ConnectException: Connection refused: connect
错误是:“在端口25的本地主机上没有侦听内容”
您正在尝试使用localhost:25作为邮件服务器,但那里没有服务器。您应该使用免费的Google SMTP服务器作为测试
mail.host=smtp.gmail.com
mail.username=//your gmail
mail.password=//your password
mail.defaultEncoding=UTF-8
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.smtp.socketFactory.port=465
接下来,使用gmail登录并打开。错误是自我解释的:
javax.mail.MessagineException:无法连接到SMTP主机:localhost,端口:25代码>
您在本地主机上没有SMTP服务器,但您在那里配置它:
// Assuming you are sending email from localhost
String host = "localhost";
...
// Setup mail server
properties.setProperty("mail.smtp.host", host);
因此,你必须:
- 将本地SMTP服务器配置为本地系统上的中继(Postfix或sendmail是两个众所周知的服务器)
- 当然,可以配置一个虚拟服务器,该服务器只跟踪邮件请求,甚至不尝试传递邮件(已知Python有这种现成的虚拟服务器)
- 或者使用允许使用的服务器配置应用程序-在公司环境中与系统管理员联系,或在个人环境中与ISP联系。无论如何,您甚至需要配置一个真正的中继
以下是工作解决方案bro。这是保证
1) 首先,打开您想发送邮件的gmail帐户,就像您的情况一样“”xyz@gmail.com"
2) 打开下面的链接
3) 单击“转到我帐户中的“不太安全的应用”部分。”选项
4) 然后打开它
5) 就是这样(:只需使用提供的解决方案:
并确保在您的google帐户上启用不太安全的应用程序访问。请问本地smtp服务器是什么?这可能很明显,但您的开发计算机上是否有一个正在运行的邮件服务器,它实际上正在侦听本地主机?apache tomcat服务器此问题的解决方案是什么?在您的计算机上配置本地smtp服务器c正确。如果是linux系统,可能安装postfix。我在应用程序中使用GlassFish服务器。@KetanGupta:AFAIK GlassFish不是SMTP服务器……请在命令行尝试
telnet localhost 25
,如果出现错误,本地没有运行SMTP服务器。我无法完全理解您的意思。请给我完整的答案,因为我不知道关于这些事情,@KetanGupta:Apache是一个HTTP服务器,它知道如何处理HTTP或HTTPS请求,并将资源或中继请求发送回另一个HTTP[S]Tomcat或GlassFish是HTTP servlet容器,它们知道如何处理HTTP请求并将其传递给servlet和筛选器。Postfix或sendmail知道如何处理SMTP请求(默认端口25)。这是一个不同的协议,由不同的服务器应用程序处理。您知道如何从开发计算机发送邮件,以及如何配置本地邮件程序吗?首先,不要设置那些socketFactory属性。其次,没有mail.password属性,也没有mail.defaultEncoding属性。有关使用Gm的正确方法,请阅读困扰