I’m using javamail package in raw format so far. I prefer to split all the communications (email, social media…) and run it as a spring REST API server. Here is the initial code snippet.
Take STS or Eclipse, Start a Spring Starter Project and follow the instructions given below.
Dependencies
Maven Dependencies are given below.
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
org.grassfield
messenger
0.0.1-SNAPSHOT
messenger
MultiPlatformMessenger
1.8
UTF-8
UTF-8
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
User.java
This is an entity class for the user.
[code language=”java”]
package org.grassfield.msg;
import org.springframework.stereotype.Component;
/**
* The Class User.
* @author Murugapandian Ramaiah
* @version 1.0
*/
@Component
public class User {
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
[/code]
MailService.java
As the name implies this is the email sending service.
[code language=”java”]
package org.grassfield.msg;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class MailService {
@Autowired
private JavaMailSender sender;
public void sendEmail (User user) {
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom(user.getEmail());
mail.setTo(user.getEmail());
mail.setSubject(“Testing Email Service”);
mail.setText(“Test email content”);
this.sender.send(mail);
}
public void sendEmailWithAttachment(User user) throws MessagingException, UnsupportedEncodingException {
MimeMessage mimeMsg = this.sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true);
helper.setFrom(new InternetAddress(user.getEmail(), user.getEmail()));
helper.setTo(user.getEmail());
helper.setSubject(“Multipart email”);
helper.setText(“Content of this email”);
this.sender.send(mimeMsg);
}
}
[/code]
MailController.java
Here is the REST controller.
[code language=”java”]
package org.grassfield.msg;
import java.io.UnsupportedEncodingException;
import javax.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* The Class MailController.
* @author Murugapandian Ramaiah
* @version 1.0
*/
@RestController
public class MailController {
/** The service. */
@Autowired
private MailService service;
/** The user. */
@Autowired
private User user;
@RequestMapping(“test-plain-text-email”)
public String testPlainTextEmail() {
user.setEmail(“recipient@office365 email address”);
service.sendEmail(user);
return “Email sent”;
}
@RequestMapping(“test-multipart-email”)
public String testMultipartEmail() throws MessagingException, UnsupportedEncodingException {
user.setEmail(“recipient@office365 email address”);
service.sendEmailWithAttachment(user);
return “Email sent”;
}
}
[/code]
application.properties
[code language=”java”]
spring.mail.host = smtp.office365.com
spring.mail.username = user@office 365 account
spring.mail.password = office 365 password
spring.mail.port=587
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.smtp.timeout=15000
spring.mail.properties.mail.smtp.connectiontimeout=15000
spring.mail.properties.mail.smtp.socketFactory.fallback = true
spring.mail.properties.mail.mail.debug=true
spring.mail.properties.mail.pop3.host=pop email address
spring.mail.properties.mail.pop3.port=110
spring.mail.properties.mail.pop3.starttls.enable=true
spring.mail.properties.mail.pop3.store=pop3s
spring.mail.properties.pop.pop3.username=pop username
spring.mail.properties.pop.pop3.password=pop password
spring.mail.properties.pop.pop3.folder=INBOX
[/code]
Test
Test your services with these.
curl -X GET -i http://localhost:8080/test-plain-text-email
curl -X GET -i http://localhost:8080/test-multipart-email
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [BM1PR0101CA0068.INDPRD01.PROD.OUTLOOK.COM]
how to fix this error tell me please
As the error says, it seems to be authentication problem. If you can provide your smtp parameters, I can dig into it.
spring.mail.host=smtp.office365.com
spring.mail.username=**************
spring.mail.password=*************
spring.mail.port=587
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.smtp.timeout=15000
spring.mail.properties.mail.smtp.connectiontimeout=15000
spring.mail.properties.mail.smtp.socketFactory.fallback = true
spring.mail.properties.mail.mail.debug=true
email authentication is done but i have some error after authentication
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 0.35250:1F001D0E, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:411B0000, 1.36674:0E000000, 1.61250:00000000, 1.45378:461B0000, 1.44866:44010000, 16.55847:0C0D0000, 17.43559:0000000024020000000000000100000000000000, 20.52176:140F698B1800103100000000, 20.50032:140F698B8B17000000000000, 0.35180:140F698B, 255.23226:03006080, 255.27962:0A000000, 255.27962:0E000000, 255.31418:0A005480, 0.35250:FB000B0E, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:3A000000, 1.36674:32000000, 1.61250:00000000, 1.45378:3F000000,
Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: