Sendgrid使用java发送邮件错误

时间:2013-08-13 12:58:52

标签: java email smtp sendgrid

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发送邮件,找到上面的代码here

在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 #

1 个答案:

答案 0 :(得分:0)

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

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

telnet smtp.sendgrid.net 587

成功之后,你可能会通过运行此命令得到类似的东西:

220 mi19 ESMTP service ready