Java 通过 Spring boot "spring-boot-starter-mail" 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33213060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Email via Spring boot "spring-boot-starter-mail"
提问by krmanish007
I am trying to send emails using spring boot, but am getting:
我正在尝试使用 spring boot 发送电子邮件,但我收到:
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:
我已经使用了这个 Maven 条目:
<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!
这似乎是一个直截了当但不要我错过了什么!
采纳答案by JeanValjean
My recommendation is to use the it.ozimov:spring-boot-email-corelibrary, that hides all these implementations behind a single component called EmailService
- well, I'm also developing the library :).
我的建议是使用it.ozimov:spring-boot-email-core库,它将所有这些实现隐藏在一个名为EmailService
- 好吧,我也在开发该库:)。
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
:
具有以下内容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, Mustacheand 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.
它还支持一些模板引擎,如Freemarker、Mustache和Pebble,同时您可以扩展它以使用更多模板引擎。此外,它还支持电子邮件调度和优先级排序(例如,密码恢复的高优先级和时事通讯的低优先级。
There is an article on LinkedIn explaining how to use it. It is here.
LinkedIn 上有一篇文章解释了如何使用它。它在这里。
回答by Andy Wilkinson
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.
你的第二个版本javax.mail.internet.MimeMessage
上除了在通过拉一个类路径spring-boot-starter-mail
。一个常见的罪魁祸首是 Geronimo 的 JavaMail 规范 jar。无论是哪个 jar,您都需要将其从应用程序的依赖项中排除。如果你不确定它来自哪里,运行你的应用程序-verbose:class
会告诉你。
回答by user2560081
Do not use javaMailSender.createMimeMessage();
try to use MimeMessagePreparator
& MimeMessageHelper
instead
不要javaMailSender.createMimeMessage();
尝试使用MimeMessagePreparator
&MimeMessageHelper
代替
回答by Christian Meyer
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.
仅供参考,您选择的端口不是任意的 - 例如,Gmail 需要 465。而且您还需要对发送 Gmail 帐户进行更改以使其正常运行。我很久以前就这样做了,但我很确定我以前见过那个例外。本示例中使用的 lambda 可能有助于解决该问题,但不幸的是我不记得了。如果您需要任何说明或想查看我正在使用的示例的更多代码,请随时与我联系。