通过Java发送邮件时出错 [英] Error in Sending mail by Java
问题描述
我的代码是:
// File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "toEmail@gmail.com";
// Sender's email ID needs to be mentioned
String from = "fromEmail@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("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
昨晚我的程序运行正常;我可以将电子邮件从任何地址发送到任何其他地址,但是现在出现此错误:
My program was working correctly last night; I could send email from any address to any other, but now this error is occuring:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at SendEmail.main(SendEmail.java:49)
第49行是:
Transport.send(message);
有人可以帮我解决此错误吗?
Can anyone help me fix this error?
我的操作系统是:Linux,Fedora 16-内核:3.3.7
My operating system is : Linux,Fedora 16 -kernel: 3.3.7
推荐答案
无法连接到SMTP主机:localhost,端口:25;
SMTP必须未在系统上运行或作为用户禁用.请与系统管理员联系,并为您启用它.
SMTP must be not running on your system or disabled for you as user. Check with your system administrator and get it enabled for you.
要检查SMTP是否在您的Linux系统上正常运行,请尝试以下命令:
To check if SMTP is working on your Linux system, try the following commands:
- 要简单地验证Sendmail是否正在运行,请尝试:
netstat -an | grep 25
- 使用telnet连接到smtp服务器的端口25:
telnet <yourhost-or-ipnumber> 25
一种. 连接到本地主机...无法打开端口25上的主机连接:连接失败.
b.如果您或您的IP被阻止,则会看到类似以下错误消息:
220-localhost ESMTP Exim 4.63#1 Fri,01 Jun 2012 19:35:30 +0530
220-我们未经授权使用此系统来传输未经请求的和/或批量电子邮件. -
echo -e "quit" | nc localhost 25
localhost.localdomain [127.0.0.1] 25(?):连接被拒绝
在外壳程序提示符下 - 邮件.
- 还有,可能更多...
- To simply verify Sendmail is running, try:
netstat -an | grep 25
- Use telnet to connect to port 25 of smtp server:
telnet <yourhost-or-ipnumber> 25
a. Connecting To localhost...Could not open connection to the host, on port 25: Connect failed.
b. if you or your ip is blocked, you would see error message something like this:
220-localhost ESMTP Exim 4.63 #1 Fri, 01 Jun 2012 19:35:30 +0530
220-We do not authorize the use of this system to transport unsolicited, and/or bulk e-mail. echo -e "quit" | nc localhost 25
localhost.localdomain [127.0.0.1] 25 (?) : Connection refused- mail at shell prompt.
- and, may be more...
您应检查sendmail
守护程序是否已启动并且始终可用.
You should check that sendmail
daemon is started and is available always.
如果您有权访问其他任何SMTP服务器,请尝试使用其SMTP主机名发送邮件,以检查您的代码段是否正常工作.
示例:String host = "smtp.gmail.com";
And if you have access to any other SMTP servers, try to send mail using their SMTP host name, to check if your code snippet is working.
Example : String host = "smtp.gmail.com";
这篇关于通过Java发送邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!