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 #