Reputation: 205
By sending an email I got the following exception:
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. b10sm22671312wmi.34 - gsmtp
Here is the code that I'm using for sending email:
MailRequest mailRequest = new MailRequest();
mailRequest.setSubject(messageByLocale.getMessage("mail.subject.forgetpassword"));
mailRequest.setTemplateName(messageByLocale.getMessage("mail.template.forgetpassword"));
mailRequest.setToEmail(tbNstyleloyalty.getEmail());
Map<String, Object> map = new HashMap<>();
map.put("tbNstyleloyalty", tbNstyleloyalty);
mailingConfig.sendEmail(mailRequest, map);
And my sendEmail
method is:
@Async
public void sendEmail(MailRequest mailRequest, Map<String, Object> model) {
MimeMessagePreparator preparator = new MimeMessagePreparator()
{
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(mailRequest.getToEmail());
message.setSubject(mailRequest.getSubject());
message.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,templatePath + mailRequest.getTemplateName() + ".vm", ApplicationConstants.CHARSET_UTF8, model),true);
}
};
this.javaMailSender.send(preparator);
}
Please, help me to overcome from this issue.
Thank you!
Upvotes: 2
Views: 3039
Reputation: 205
Thank you guys for your answers. All answers given by you were correct but I was unable to understand where I should add this property.
Finally I add:
spring.mail.properties.mail.smtp.starttls.enable = true
to my property file and then I got success.
Upvotes: 4
Reputation: 4912
i think below code is simple enough to be used for mailing functionality
//headers
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//code
Session mailSession = new SmtpImpl().getSession();
Message simpleMessage = new MimeMessage(mailSession);
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
try {
simpleMessage.setFrom(new InternetAddress("from address")));
simpleMessage.addRecipient(RecipientType.TO, new InternetAddress("to address"));
String SENDCC_GET = "[email protected],person2.gmail.com";
String [] SENDCC = SENDCC_GET.split(",");
InternetAddress[] addressCC = new InternetAddress[SENDCC.length];
for (int i = 0; i < SENDCC.length; i++) {
addressCC[i] = new InternetAddress(SENDCC[i]);
}
//for bcc
simpleMessage.setRecipients(Message.RecipientType.BCC, addressCC);
//for cc
//simpleMessage.setRecipients(Message.RecipientType.CC, addressCC);
String message = null;
String subject = null;
subject = "email subject";
simpleMessage.setSubject(subject);
message = "message body";
messageBodyPart.setContent(message, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
simpleMessage.setContent(multipart);
Transport.send(simpleMessage);
} catch (AddressException e) {
LOGGER.error("******Error while sending - Email: Address Exception::: " + e);
} catch (MessagingException e) {
LOGGER.error("******Error while sending - Email: Messaging Exception::: " + e);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
//SmtpImpl.java
public Session getSession(){
Properties props = new Properties();
props.put("mail.smtp.host", EmailUtilityParam.getSmtpHostName());
props.put("mail.smtp.port", EmailUtilityParam.getSmtpPort());
props.put("mail.smtp.user", EmailUtilityParam.getAuthenticationId());
props.put("mail.smtp.password", EmailUtilityParam.getAuthenticationPassword());
props.put("mail.debug", "false");
Session mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(EmailUtilityParam
.getAuthenticationId(), EmailUtilityParam
.getAuthenticationPassword());
}
});
return mailSession;
}
SMTP dev tool can be used for development and keep it in default values for testing whether it works
HOST = localhost
PORT = 25
USER(authentication id) = //empty string
PASSWORD(authentication password) = //empty string
Upvotes: 0
Reputation: 3709
Probably you are missing either of the 2 points:
props.put("mail.smtp.starttls.enable", "true");
Upvotes: 1
Reputation: 49
I guess that your mail server uses STARTSSL for authentication (as the exception message implies).
Maybe this post helps: JavaMail smtp properties (for STARTTLS)
Upvotes: 0