java gmail smtp_java – 使用spring boot使用gmail smtp发送电子邮件

我正在尝试使用spring boot发送电子邮件.我有3个文件如下

MailSender.java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

public class MailSender

{

@Autowired

private static JavaMailSender mailSender;

public static void main(String[] args)

{

sendemail();

}

public static void sendemail() {

MimeMessage mail = mailSender.createMimeMessage();

try {

MimeMessageHelper helper = new MimeMessageHelper(mail, true);

helper.setTo("emailsendertester24@gmail.com");

//helper.setReplyTo("someone@localhost");

helper.setFrom("emailsendertester24@gmail.com");

helper.setSubject("Lorem ipsum");

helper.setText("Lorem ipsum dolor sit amet [...]");

} catch (MessagingException e) {

e.printStackTrace();

} finally {}

mailSender.send(mail);

//return helper;

}

}

的pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

Example

SendingEmail

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

1.3.2.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-mail

javax.mail

mail

1.4.5

和application.properties

spring.mail.host=smtp.gmail.com

spring.mail.username=*****

spring.mail.password=*****

spring.mail.properties.mail.smtp.auth = true

spring.mail.properties.mail.smtp.socketFactory.port = 25

spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory

spring.mail.properties.mail.smtp.socketFactory.fallback = false

spring.mail.properties.mail.smtp.ssl.enable=true

所以现在当我尝试运行我的MailSender类时,它给我一个错误“线程中的异常”主“java.lang.NullPointerException”在线MimeMessage mail = mailSender.createMimeMessage();

我知道mailSender属性为null,因为它无法从application.properties文件中获取属性.

解决方法:

它还提供了有用的功能,如调度,模板引擎支持和REDIS持久性.

如果您需要发送纯文本电子邮件,则只需导入核心模块即可

it.ozimov

spring-boot-email-core

0.5.0

注释主要如下:

package com.myapplication;

@SpringBootApplication

@EnableEmailTools

public class MainApplication {

public static void main(final String... args) {

}

}

在application.yml(o properties)中提供配置,并在组件类中自动装配EmailService,例如

@Autowired

public EmailService emailService;

public void sendEmailWithoutTemplating(){

final Email email = EmailImpl.builder()

.from(new InternetAddress("cicero@mala-tempora.currunt", "Marco Tullio Cicerone "))

.to(Lists.newArrayList(new InternetAddress("titus@de-rerum.natura", "Pomponius Attĭcus")))

.subject("Laelius de amicitia")

.body("Firmamentum autem stabilitatis constantiaeque eius, quam in amicitia quaerimus, fides est.")

.encoding(Charset.forName("UTF-8")).build();

emailService.send(email);

}

可以在项目的GitHub存储库中找到更多示例.此外,LinkedIn上有this article.

标签:java,spring,spring-boot-2

来源: https://codeday.me/bug/20190528/1167925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值