Hejka.
Mam program, który wysyła bez problemu maile, jeśli serwer jest nie autentykowany.
Musiałem dodać mu możliwość wysyłania maili z serwerów komercyjnych (onet.pl, interia itp. itd.).
Tam występuje autentykacja.
Obsłużyłem wiec ją na podstawie dokumentacji i przykładów z sieci.
Co się okazało, przy próbie wysłania maila sypie connection refused.
Dodam, że nie da się telnetować na ten serwer (smtp.poczta.onet.pl), ale za to mój outlook działa normlanie...
So... o co chodzi??
public static int sendMail(String smtpServer, IMail mail) {
try {
final IMail fmail = mail;
Properties props = new Properties();
Session session = null;
props.put("mail.smtp.host", smtpServer);
if(mail.isAuth()){
props.put("mail.smtp.auth", true);
props.put("mail.user", mail.getUser());
props.put("mail.password", mail.getPasswd());
session = Session.getDefaultInstance(props, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fmail.getUser(), fmail.getPasswd());
}
});
}else{
session = Session.getDefaultInstance(props, null);
}
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(mail.getAddressFrom()));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail.getAddressTo(), false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(StringsConstants.MAIL_TITLE_PREFIX + " " + mail.getTitle());
msg.setText(mail.getBody());
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
// Transport.send(msg);
Transport t = session.getTransport("smtp");
t.connect(smtpServer, mail.getUser(), mail.getPasswd());
t.send(msg, msg.getRecipients(Message.RecipientType.TO));
t.close();
logger.info(LogsConstants.INFO_MAIL_WAS_SEND + " Od: " + mail.getAddressFrom() + " Do: " + mail.getAddressTo()
+ " Tytuł: " + mail.getTitle());
return 0;
}
catch (Exception e) {
e.printStackTrace();
logger.error(LogsConstants.ERROR_SENDING_EMAIL, e);
return 1;
}
Proszę o pomoc, bo mi się kończą pomysły.