Reputation: 7189
I am trying to send emails using spring boot, but am getting:
java.lang.UnsupportedOperationException: Method not yet implemented
at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:89)
at org.springframework.mail.javamail.SmartMimeMessage.<init>(SmartMimeMessage.java:52)
at org.springframework.mail.javamail.JavaMailSenderImpl.createMimeMessage(JavaMailSenderImpl.java:325)
I have used this maven entry:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.port= 25
spring.mail.username= test
spring.mail.password= test
And My code:
@Autowired
private JavaMailSender javaMailSender;
private void send() {
MimeMessage mail = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo("[email protected]");
helper.setReplyTo("someone@localhost");
helper.setFrom("someone@localhost");
helper.setSubject("Lorem ipsum");
helper.setText("Lorem ipsum dolor sit amet [...]");
} catch (MessagingException e) {
e.printStackTrace();
} finally {}
javaMailSender.send(mail);
//return helper;
}
This appears to be a straight forward but don't what am I missing!
Upvotes: 19
Views: 75833
Reputation: 625
This worked for me:
private TemplateEngine templateEngine;
@Autowired
private JavaMailSender mailSender;
@Autowired
public MailContentBuilder mailContentBuilder;
public void sendEmail(Users user, VerificationToken verificationToken) throws Exception {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
String name = user.getFirstname();
String vtoken = verificationToken.getVtoken();
String url = "http://3.16.214.183:8888/home/".concat(String.valueOf(user.getUserid())).concat("/").concat(vtoken);
String content = mailContentBuilder.build(name, url);
helper.setTo(user.getEmail());
helper.setSubject("AppName - Please Verify Your Email");
helper.setText(content, true);
};
try {
mailSender.send(messagePreparator);
} catch (MailException e) {
e.printStackTrace();
}
}
It only seemed to require this dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
And I set properties:
spring.mail.host=smtp.gmail.com
spring.mail.port=465
[email protected]
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
FYI, the port you choose is not arbitrary - 465 was required for Gmail, for example. And you also needed to make changes to the sending Gmail account to get it to function properly. I did this a long time ago, but I'm positive I've seen that exception before. The lambda used in this example may be helpful in resolving that issue, but unfortunately I can't remember. Feel free to contact me if you need any clarification or would like to see more code of the example that I got working.
Upvotes: 0
Reputation: 11
Do not use javaMailSender.createMimeMessage();
try to use MimeMessagePreparator
& MimeMessageHelper
instead
Upvotes: 1
Reputation: 17713
My recommendation is to use the it.ozimov:spring-boot-email-core library, that hides all these implementations behind a single component called EmailService
- well, I'm also developing the library :).
Your example would be:
@Autowired
public EmailService emailService;
public void sendEmail(){
final Email email = DefaultEmail.builder()
.from(new InternetAddress("[email protected]"))
.replyTo(new InternetAddress("someone@localhost"))
.to(Lists.newArrayList(new InternetAddress("someone@localhost")))
.subject("Lorem ipsum")
.body("Lorem ipsum dolor sit amet [...]")
.encoding(Charset.forName("UTF-8")).build();
emailService.send(email);
}
With the following application.properties
:
spring.mail.host=your.smtp.com
spring.mail.port=587
spring.mail.username=test
spring.mail.password=test
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
It also supports some template engines, like Freemarker, Mustache and Pebble, while you can extend it to use more template engines. Moreover, it also supports email scheduling and prioritization (e.g. high priority for password recovery and low priority for newsletter.
There is an article on LinkedIn explaining how to use it. It is here.
Upvotes: 31
Reputation: 116241
You have a second version of javax.mail.internet.MimeMessage
on the classpath in addition to the one that's pulled in via spring-boot-starter-mail
. A common culprit is Geronimo's JavaMail spec jar. Whichever jar it is, you need to exclude it from your application's dependencies. If you're not sure where it's coming from, running your application with -verbose:class
will tell you.
Upvotes: 18