Reputation: 11
I am currently working on forgot password feature and I am using javax.mail. I tried smtp.gmail.com
as my host and it worked fine in local and live server. The link below, you can see there's a mailed-by and signed-by gmail
.
However, using smtpout.secureserver.net
will only work in local server.
In live server, it says Could not connect to SMTP host: smtpout.secureserver.net, port: 465, response: 554
. And, I don't see signed-by and mailed-by
when viewing the email even in local server.
Do I really need to buy a certificate from GoDaddy to seesigned-by and mailed-by
? Also, could this be the reason why it won't work in live server? I am sorry I'm really new to this.
Here's the code:
public class Mailer {
Message message;
Transport transport;
Properties props;
private String userName = "[email protected]";
private String passWord = "password@123";
private String protocol = "smtp";
private String host = "smtpout.secureserver.net";
private String port = "465";
private String socketFactoryClass = "javax.net.ssl.SSLSocketFactory";
private final String
MAIL_SMTP_START_TLS_ENABLE = "mail.smtp.starttls.enable",
MAIL_SMTP_AUTH = "mail.smtp.auth",
MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol",
MAIL_DEBUG = "mail.debug",
MAIL_SMTP_PORT = "mail.smtp.port",
MAIL_SMTP_HOST = "mail.smtp.host",
MAIL_SMTP_SOCKETFACTORY_PORT = "mail.smtp.socketFactory.port",
MAIL_SMTP_SOCKETFACTORY_CLASS = "mail.smtp.socketFactory.class",
MAIL_SMTP_SSL_ENABLE = "mail.smtp.ssl.enable";
public Mailer() {
setProperties();
}
public void setProperties() {
props = System.getProperties();
props.put(MAIL_SMTP_AUTH, true);
// props.put(MAIL_SMTP_START_TLS_ENABLE, true);
props.put(MAIL_DEBUG, true);
props.put(MAIL_TRANSPORT_PROTOCOL, protocol);
props.put(MAIL_SMTP_PORT, port);
props.put(MAIL_SMTP_HOST, host);
props.put(MAIL_SMTP_SSL_ENABLE, true);
// props.put(MAIL_SMTP_SOCKETFACTORY_PORT, String.valueOf(port) );
// props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, socketFactoryClass);
}
public void sendPasswordToEmail (String recipientEmail, User user) throws Exception {
Session session = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(userName));
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
message.setSubject("Forgot Password Request");
StringBuffer messageBuffer = new StringBuffer();
messageBuffer.append("<html><h3>My Web App Portal</h3>");
messageBuffer.append("Here's your login details: <br><br>");
messageBuffer.append("<b>Username:</b> "+user.getLoginUser()+"<br>");
messageBuffer.append("<b>Password:</b> "+user.getPassword()+"<br><br> </html>");
message.setContent(messageBuffer.toString(), "text/html; charset=utf-8");
message.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(AuthenticationFailedException ex) {
System.out.println(ex.getMessage());
}
}
Thank you very much.
Upvotes: 0
Views: 652
Reputation: 372
My configuration in SpringBoot (replace domainname.com to your domainname and password)
spring:
mail:
host: smtpout.secureserver.net
port: 465
username: [email protected]
password: password
protocol: smtp
properties.mail.smtp:
auth: true
ssl.enable: true
ssl.trust: "*"
And I had to add mimeMessageHelper.setFrom("[email protected]");
before sending the mail (else it was taking my system name and gave an error) and this setup worked.
Upvotes: 2