我正在使用java郵件api發送郵件。我可以使用Google帳戶發送郵件,但面臨使用SSL從我的域發送的某些問題。無法從我的主機發送郵件
這是實施,請讓我知道我缺乏的地方。
// Create all the needed properties
Properties connectionProperties = new Properties();
// SMTP host
connectionProperties.put("mail.smtp.host", "abc.example.com");
// Is authentication enabled
connectionProperties.put("mail.smtp.auth", "true");
// Is StartTLS enabled
connectionProperties.put("mail.smtp.starttls.enable", "true");
// SSL Port
connectionProperties.put("mail.smtp.socketFactory.port", "465");
// SSL Socket Factory class
connectionProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// SMTP port, the same as SSL port :)
connectionProperties.put("mail.smtp.port", "465");
// Create the session
Session session = Session.getDefaultInstance(connectionProperties, new javax.mail.Authenticator()
{ // Define the authenticator
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]", "abcxdsjdf123");
}
});
System.out.println("done!");
// Create and send the message
try
{
// Create the message
Message message = new MimeMessage(session);
// Set sender
message.setFrom(new InternetAddress("[email protected]"));
// Set the recipients
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
// Set message subject
message.setSubject("Hello from Test");
// Set message text
message.setText("This is test mail;)");
System.out.print("Sending message...");
// Send the message
Transport.send(message);
System.out.println("done!");
}
catch (MessagingException e)
{
System.out.println(e.toString());
}
下面是我收到的例外:
javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
任何形式的幫助將是明顯的。 在此先感謝。
我想你do't提及關於協議props.put( 「SMTP」 「mail.transport.protocol。」);把這個放在末尾 – Sree
你現在使用哪個主機? –
我使用magemojo主機 –