首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java邮件可以使用gmail作为主机,但不能使用godaddy

问Java邮件可以使用gmail作为主机,但不能使用godaddy
EN

Stack Overflow用户
提问于 2018-05-17 19:24:33
回答 1查看 318关注 0票数 0

我目前正在开发忘记密码功能,我正在使用javax.mail。我尝试使用smtp.gmail.com作为我的主机,它在本地和实时服务器上都工作得很好。在下面的链接中,您可以看到有一个mailed-by and signed-by gmail。

Image

但是,使用smtpout.secureserver.net只能在本地服务器上工作。

在实时服务器中,它显示为Could not connect to SMTP host: smtpout.secureserver.net, port: 465, response: 554。而且,即使在本地服务器上查看电子邮件时,我也看不到signed-by and mailed-by。

我真的需要从GoDaddy购买证书才能看到signed-by and mailed-by吗?另外,这可能是它不能在实时服务器上工作的原因吗?很抱歉,我真的是个新手。

代码如下:

代码语言:javascript
复制
public class Mailer {
    Message message;
    Transport transport;
    Properties props;

    private String userName = "my@email.com";
    private String passWord = "password@123";
    private String protocol = "smtp";
    private String host = "smtpout.secureserver.net";
    private String port = "465";
    private String socketFactoryClass = "javax.net.ssl.SSLSocketFactory";


    private final String
        MAIL_SMTP_START_TLS_ENABLE          =           "mail.smtp.starttls.enable",
        MAIL_SMTP_AUTH                      =           "mail.smtp.auth",
        MAIL_TRANSPORT_PROTOCOL             =           "mail.transport.protocol",
        MAIL_DEBUG                          =           "mail.debug",
        MAIL_SMTP_PORT                      =           "mail.smtp.port",
        MAIL_SMTP_HOST                      =           "mail.smtp.host",
        MAIL_SMTP_SOCKETFACTORY_PORT        =           "mail.smtp.socketFactory.port",
        MAIL_SMTP_SOCKETFACTORY_CLASS       =           "mail.smtp.socketFactory.class",
        MAIL_SMTP_SSL_ENABLE                =           "mail.smtp.ssl.enable";

 public Mailer() {
        setProperties();
    }

    public void setProperties() {
        props = System.getProperties();
        props.put(MAIL_SMTP_AUTH, true);
//      props.put(MAIL_SMTP_START_TLS_ENABLE, true);
        props.put(MAIL_DEBUG, true);
        props.put(MAIL_TRANSPORT_PROTOCOL, protocol);
        props.put(MAIL_SMTP_PORT, port);
        props.put(MAIL_SMTP_HOST, host);
        props.put(MAIL_SMTP_SSL_ENABLE, true);

//      props.put(MAIL_SMTP_SOCKETFACTORY_PORT, String.valueOf(port) );
//      props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, socketFactoryClass);       
    }

public void sendPasswordToEmail (String recipientEmail, User user) throws Exception {
    Session session = Session.getInstance(props,null);
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(userName));
        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));         
        message.setSubject("Forgot Password Request");

        StringBuffer messageBuffer = new StringBuffer();
        messageBuffer.append("<html><h3>My Web App Portal</h3>");        
        messageBuffer.append("Here's your login details: <br><br>");
        messageBuffer.append("<b>Username:</b> "+user.getLoginUser()+"<br>");
        messageBuffer.append("<b>Password:</b> "+user.getPassword()+"<br><br> </html>");
        message.setContent(messageBuffer.toString(), "text/html; charset=utf-8");

        message.setSentDate(new Date());

        Transport transport = session.getTransport("smtp");
        transport.connect(host, userName, passWord);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }catch(AuthenticationFailedException ex) {
        System.out.println(ex.getMessage());
    }
}

非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2020-04-18 20:59:41

我在SpringBoot中的配置(将domainname.com替换为您的域名和密码)

代码语言:javascript
复制
spring:
  mail:
    host: smtpout.secureserver.net
    port: 465
    username: info@domainname.com
    password: password
    protocol: smtp
    properties.mail.smtp:
      auth: true
      ssl.enable: true
      ssl.trust: "*"

在发送邮件之前,我必须添加mimeMessageHelper.setFrom("info@domainname.com"); (否则它会获取我的系统名称并给出一个错误),并且此设置可以正常工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50390231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档