在通过提供相同问题的帖子后,我编写了以下代码。但我得到以下例外:
javax.mail.MessagingException:
无法连接到SMTP主机:smtp.gmail.com,port:587;
嵌套异常是:java.net.ConnectException:连接超时:连接
public static void main(String[] args) {
String to = "xxx@gmail.com" // valid gmail address.
String from = "yyy@gmail.com"; // valid gmail address
String host = "smtp.gmail.com";
String password = "****"; // password of the gmaill acc used in from
int port = 587;
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host",host );
properties.setProperty("mail.smtp.user", from);
properties.setProperty("mail.smtp.password", password);
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Mail");
message.setText("This is just a test mail generated");
Transport transport = session.getTransport("smtp");
transport.connect(host,from,password);
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress(to);
transport.sendMessage(message,addresses);
System.out.println("Message Sent Successfully");
}catch(MessagingException excp){
System.out.println(excp);
}
}
有人能说出我正在做的错误。我的Gmail帐户中是否有任何设置需要设置为使用gmail smtp服务器?
答案 0 :(得分:1)
存在连接问题。首先检查连接到“smtp.gmail.com”。
转到命令提示符并运行ping命令,如下所示。
ping smtp.gmail.com
如果您没有收到服务器的回复,则可能存在防火墙问题。
答案 1 :(得分:1)
尝试以下代码。你需要下载javax.mail
包(一个jar文件),我假设你已经有了这个jar文件,因为你已经尝试过这个代码,因此,我没有提供下载该jar文件的链接。正确设置类路径并导入必要的包。请注意防火墙和您选择的主机端口。
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
final class MailClient
{
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;
}
}
public void mail()
{
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);
}
}
}
}
final public class Main
{
public static void main(String...args)
{
new MailClient().mail();
}
}
答案 2 :(得分:0)
连接超时表示它甚至没有连接,这意味着它甚至无法访问您的Gmail帐户,因此在那里设置一些内容无济于事。
你可能想尝试一个简单的telnet到那个主机/端口,看看你是否可以连接。如果你不能,你可能会错误地连接位置,或者可能出现防火墙问题。