Reputation: 12728
I am sending mails in a Java EE project using Javamail and I am testing in localhost. I can send the mail the first time I use the code below, but the second time I cannot, and the console shows it tries to connect to localhost, which doesn't happen the first time.
1st time console messages:
DEBUG: JavaMail version 1.5.1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.1and1.es", port 465, isSSL false
220 kundenserver.de (mreue104) Nemesis ESMTP Service ready
DEBUG SMTP: connected to host "smtp.1and1.es", port: 465
....(more host/connection/email content details, but the connection is established)
2nd time console message:
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
And it stops and does nothing.
properties
(smtp segment):
mail.smtp.protocol=smtp
mail.smtp.useTSL=true
mail.smtp.auth=true
#mail.smtp.host=smtp.redfinanciera.es
mail.smtp.host=smtp.1and1.es
mail.smtp.port=465
mail sender code:
public CCorreo mandarCorreo(CCorreoForm form) {
InputStream input = null;
Properties prop = null;
CCorreo correo = null;
try {
//leer prop y mandar mensajes
input = MailSmtpSender.class.getResourceAsStream("/config/properties/mail.properties");
prop = new Properties();
prop.load(input);
final String smtpUser = prop.getProperty("mail.smtp.user");
final String smtpPassword = prop.getProperty("mail.smtp.pass");
//prop setting de smtp.
prop.setProperty("mail.smtp.connectiontimeout", "5000");
prop.setProperty("mail.smtp.timeout", "5000");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.debug", "true");
//socket factory port: default if null
prop.put("mail.smtp.socketFactory.port", "465");
// prop.put("mail.smtp.socketFactory.port", "*");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.socketFactory.fallback", "false");
//crear una sesion. con autentificacion SSL
Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUser, smtpPassword);
}
});
//construir message
MimeMessage message = new MimeMessage(session);
message.setFrom(prop.getProperty("mail.smtp.from"));
if (!StringUtils.isEmpty(form.getCuerpo())){
message.setText(form.getCuerpo(), "UTF8");
} else {
message.setText("");
}
message.setSubject(form.getAsunto(), "UTF8");
//construir receptor. sacar de form.
String[] addressesString = {form.getReceptor()};
InternetAddress addresses[] = new InternetAddress[addressesString.length];
for (int i = 0; i < addressesString.length; i++){
addresses[i] = new InternetAddress(addressesString[i].trim().toLowerCase());
}
message.setRecipients(Message.RecipientType.TO, addresses);
//formar el correo, rellenar todas las partes de el.
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(form.getCuerpo(), "text/html;charset=utf-8");
mp.addBodyPart(mbp);
message.setContent(mp);
message.setSentDate(new Date());
//establecer un Transport y mandar el mensaje
Transport.send(message);
} catch (Exception e) {
throw new RuntimeException("Se ha producido un error al mandar el correo. Mensaje: " + e.getMessage());
} finally {
prop.clear();
try {
input.close();
} catch (IOException e) {
System.out.println("Se ha producido un error al mandar el correo. Mensaje: " + e.getMessage());
}
}
return correo;
}
So here's the question: why does it connect to smtp server using port 465 the first time, but in second try it connects to localhost and using port 25??? It's totally weird.
Edit:
I open a lightbox/modal in AngularJS to send a email. Once the email is sent the lightbox is closed and the main page is shown again. I found that if I don't refresh the main page, I cannot send an email again, but if I do a refresh, it works fine. I am trying to figure out why it is, and hoping to find answers about this, too.
Upvotes: 0
Views: 1399
Reputation: 29961
Your code contains many of the common JavaMail mistakes described in the JavaMail FAQ.
In particular, your problem is most likely caused by your use of Session.getDefaultInstance instead of Session.getInstance, but you'll want to fix all the problems in the above link.
Upvotes: 1