繁体   English   中英

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

[英]Spring Boot - Sending Email using Gmail

需要使用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!";
}

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

请改用此代码段

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);

}

}

使用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;
    }

}

由于根本的问题是套接字超时,因此最有可能是防火墙阻止了您直接连接。 JavaMail FAQ包含连接调试技巧有关通过代理服务器进行连接的说明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM