Spring Boot-使用Gmail发送电子邮件

时间:2018-12-14 11:14:05

标签: java spring email spring-boot javamail

需要使用Spring Boot从gmail发送邮件

但是我遇到了类似的错误

此应用程序没有针对/ error的显式映射,因此您将其视为备用。

发生意外错误(类型=内部服务器错误,状态= 500)。 邮件服务器连接失败;嵌套异常是com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;无法连接到主机。超时5000;嵌套的异常是:java.net.SocketTimeoutException:连接超时。失败消息:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;超时5000;嵌套的异常是:java.net.SocketTimeoutException:连接超时

application.properties

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=XXXXXXXXXX@gmail.com
spring.mail.password=********
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

控制器

@Autowired
private JavaMailSender sender;

@RequestMapping(value="/sendMail/{mail}")
public String sendMail(@PathVariable String mail) {
    MimeMessage message = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);

    try {
        helper.setTo(mail);
        helper.setText("Greetings :)");
        helper.setSubject("Mail From Spring Boot");
    } catch (MessagingException e) {
        e.printStackTrace();
        return "Error while sending mail ..";
    }
    sender.send(message);
    return "Mail Sent Success!";
}

在邮件设置中还允许安全性较低的应用

3 个答案:

答案 0 :(得分:0)

  

请改用此代码段

public class ExceptionMailer{
    private String smtpHost;
    private String smtpPort;
    private String senderMail;
    private String password;
    static final Properties props = new Properties();

ExceptionMailer(){
    this.smtpHost = "smtp.gmail.com";
    this.smtpPort = "465";
    this.senderMail = "xyz@gmail.com";
    this.password = "xxxxxxxx";

    props.put("mail.smtp.host", this.smtpHost);
    props.put("mail.smtp.socketFactory.port", this.smtpPort);
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", this.smtpPort);
}

public void send(){
           //get authentication 
        Session session = Session.getDefaultInstance(props,new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication(senderMail, password);
                }
        });

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderMail));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));
message.setSubject(MAIL_SUBJECT);
message.setContent("This is a message", "text/html; charset=utf-8");
Transport.send(message);

}

}

答案 1 :(得分:0)

要使用Spring发送电子邮件,必须具备以下属性。

spring.mail.host=
spring.mail.port=465
spring.mail.protocol=smtps

#Mail server Username & Password
spring.mail.username=
spring.mail.password=

spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtps.auth=true
spring.mail.properties.mail.smtps.starttls.enable=true
spring.mail.properties.mail.smtps.timeout=8000

您可以参考下面的类以使用Spring发送电子邮件。

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class SendEmailService  {

    @Autowired
    private JavaMailSender mailSender;

    public void sendEmail(final UserObject userObject) {            

        String toAddress = userObject.getEmail();
        String subjectText = userObject.getSubject();
        SimpleMailMessage emailMessage = composeEmail(toAddress, subjectText);

        mailSender.send(emailMessage);
    }

    private SimpleMailMessage composeEmail(final String toAddress, final String subjectText) {

        final SimpleMailMessage email = new SimpleMailMessage();    
        email.setTo(toAddress);
        email.setSubject(subjectText);
        email.setText("Some Text");
        email.setFrom("From Address");

        return email;
    }

}

答案 2 :(得分:0)

由于根本问题是套接字超时,因此最有可能是防火墙阻止了您直接连接。 JavaMail常见问题解答包含connection debugging tipsconnecting through a proxy server的说明。