如何在Springboot aplication中使用Godaddy webmail发送电子邮件

时间:2017-10-26 08:05:10

标签: java spring

何时可以使用Gmail成功发送电子邮件,但当我使用Godaddy网络邮件时,我会收到以下信息:

Failed message 1: javax.mail.MessagingException: Could not connect to SMTP 
host: smtpout.asia.secureserver.net, port: 465, response: -1] with root 
cause
javax.mail.MessagingException: Could not connect to SMTP host: 
smtpout.asia.secureserver.net, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2106) ~
[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712) ~
[javax.mail-1.5.6.jar:1.5.6]
at javax.mail.Service.connect(Service.java:366) ~[javax.mail-
1.5.6.jar:1.5.6]

这是我的属性文件

spring.mail.host =  smtpout.asia.secureserver.net
spring.mail.username=  xyz@domian.com
spring.mail.password= ******

spring.mail.properties.mail.transport.protocol=smtp

spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timeout=60000
spring.mail.properties.mail.imap.ssl.enable=true
spring.mail.properties.mail.imap.ssl.trust=*

这是我的代码

//SendMail using java Mail API
@Autowired
JavaMailSender mailSender;

public String send(String to, String Subject, String Password) throws 
MessagingException{

    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper;

    helper = new MimeMessageHelper(message, true);

    helper.setSubject(Subject);
    helper.setTo(to);
    helper.setText("Your password is:"+Password);
    mailSender.send(message);
    return "false";
}

4 个答案:

答案 0 :(得分:0)

此问题与JAVA代码无关,它似乎在使用SMTP协议的服务器设置上出现问题。

但是我怀疑你可能在JAVA的SMTP配置中遗漏了一些东西,我会在下面指出它。

  • 将本地SMTP服务器配置为本地的中继,只是为了 检查你是否正确。
  • 您甚至可以配置虚拟服务器来跟踪电子邮件请求。
  • 检查您的防火墙设置,可能会受到限制 用于其他SMTP服务器。

答案 1 :(得分:0)

我遇到了同样的问题-快要疯了-当我在Stackoverflow中找到一个可行的解决方案时,尽管我已经失去了链接,对不起。

在这个(旧式)项目中,我使用的是Spring3.x。邮件是通过org.springframework.mail.javamail.JavaMailSenderImpl发送的,并且配置如下:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.server.host}" />
    <property name="port" value="${mail.server.port}" />
    <property name="protocol" value="${mail.server.protocol}" />
    <property name="username" value="${mail.server.username}" />
    <property name="password" value="${mail.server.password}" />
    <property name="javaMailProperties">
        <util:properties location="classpath:javamail.properties" />
    </property>
</bean>

有人建议使用此配置:

mail.server.protocol=smtps
mail.smtps.quitwait=false
mail.smtp.auth=true
mail.smtp.starttls.enable=true

那很好。

请注意,某些属性中的协议为 smtps ,而其他属性中的协议为smtp。

答案 2 :(得分:0)

Thakur!

我遇到了与您完全相同的错误,但是现在我有了基于您的代码的解决方案。

尝试在您的媒体资源中添加以下内容:

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

您的问题正在发生,因为GoDaddy在其连接请求中要求使用SSL,否则他们将无法回答您的请求!另外,我使用https://www.smtper.net测试了我的smtp连接,看看它是否有效。您可以使用它来检查您的问题是否也是缺少的SSL。

答案 3 :(得分:0)

我正在使用Spring Boot 2.2,并且可以使用以下配置:

spring.mail.host=smtpout.secureserver.net
spring.mail.port=465
spring.mail.username=your_email@example.com
spring.mail.password=your_password
# Other properties
spring.mail.properties.mail.smtp.auth=true
# TLS, port 587
#spring.mail.properties.mail.smtp.starttls.enable=true
# SSL, port 465
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

当您使用gmail时,协议通常为TLS,但GoDaddy看起来仅使用SSL,因此我注释了TLS配置并启用了SSL配置。

发送电子邮件(在spring.mail.username中相同)时,必须设置来自的字段,否则会出现错误:

SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("your_email@example.com");
...

希望可以帮助您。

PD:在Google搜寻了很多之后,我在https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/

中找到了解决方案
相关问题