Ciao a tutti,
ho provato a inviare un E-Mail utilizzando un programma java. La classe da me utilizzata è quella di seguito riportata. Continuo ad avere il seguente errore:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
mentre se utilizzo la porta di comunicazione 465 ottengo l'errore:
ava.net.ConnectException: Connection timed out: connect
Mi potreste dire come risolvere il mio problema?
public class SendMailUsingAuthentication
{
private static final String SMTP_HOST_NAME = "out.alice.it";
private static final String SMTP_AUTH_USER = "Myuser";
private static final String SMTP_AUTH_PWD = "Mypassword";
private static final String emailMsgTxt = "Online Order Confirmation Message. Also include the Tracking Number.";
private static final String emailSubjectTxt = "Order Confirmation Subject";
private static final String emailFromAddress = "e-mail";
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = {"email", "email"};
public static void main(String args[]) throws Exception
{
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail( String recipients[ ], String subject,
String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
/*props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");*/
props.put("mail.smtp.user", "Myuser");
props.put("mail.smtp.password", "Mypassword");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.host", "out.alice.it");
props.put("mail.smtp.port", "25");
//props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.socketFactory.class","javax.n et.ssl.SSLSocketFactory");
//props.put("mail.smtp.socketFactory.fallback", "false");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
//session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
try
{
Transport.send(msg);
}
catch(Exception ex)
{
System.out.print(ex);
}
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}

Rispondi quotando