-2
public class SimpleMail {

private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
private static final String SMTP_AUTH_USER = "Myusername";
private static final String SMTP_AUTH_PWD  = "Password";

public static void main(String[] args) throws Exception{
   new SimpleMail().test();
}

public void test() throws Exception{
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", "465");
    Authenticator auth = new SMTPAuthenticator();
    Session mailSession = Session.getDefaultInstance(props, auth);
    // uncomment for debugging infos to stdout     

    mailSession.setDebug(true);        

    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);

    Multipart multipart = new MimeMultipart("alternative");

    BodyPart part1 = new MimeBodyPart();
    part1.setText("This is multipart mail and u read part1......");

    BodyPart part2 = new MimeBodyPart();
    part2.setContent("<b>This is multipart mail and u read part2......</b>","text.html");

    multipart.addBodyPart(part1);
    multipart.addBodyPart(part2);

    message.setContent(multipart);
    message.setFrom(new InternetAddress("aKrishiv@absinessware.com"));
    message.setSubject("This is the subject");
    message.addRecipient(Message.RecipientType.TO,new InternetAddress("someone@somewhere.com"));

    transport.connect();
    transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
    transport.close();`

有人可以帮助我吗?我正在尝试使用 SMTP 发送邮件,上面的代码可以在这里找到

在eclipse上运行代码时出现此错误

DEBUG: setDebug: JavaMail version 1.5.0 DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true Username: Myusername Password:
Mypassword DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying
to connect to host "smtp.sendgrid.net", port 587, isSSL false
Exception in thread "main" com.sun.mail.util.MailConnectException:
Couldn't connect to host, port: smtp.sendgrid.net, 587; timeout -1;
nested exception is: java.net.ConnectException: Connection timed out:
connect at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:367) at
javax.mail.Service.connect(Service.java:226) at
javax.mail.Service.connect(Service.java:175) at
SimpleMail.test(SimpleMail.java:53) at
SimpleMail.main(SimpleMail.java:14) Caused by:
java.net.ConnectException: Connection timed out: connect at
java.net.DualStackPlainSocketImpl.connect0(Native Method) at
java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at
java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at
java.net.AbstractPlainSocketImpl.connect(Unknown Source) at
java.net.PlainSocketImpl.connect(Unknown Source) at
java.net.SocksSocketImpl.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 6 more #
4

1 回答 1

0

错误信息表示连接邮件服务器(主机:smtp.sendgrid.net,端口 587)时连接失败。

您应该确保您可以连接到您机器上的 smtp.sendgrid.net。您可以在运行测试的机器上尝试此命令以验证网络连接:

telnet smtp.sendgrid.net 587

成功后,您可能会通过运行以下命令得到类似我刚刚得到的信息:

220 mi19 ESMTP service ready
于 2013-08-13T13:19:29.707 回答