Java 邮件使用 gmail 作为主机,但不是 godaddy

  • 本文关键字:主机 godaddy gmail Java java
  • 更新时间 :
  • 英文 :


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

图像

但是,使用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吗?另外,这可能是它无法在实时服务器中工作的原因吗?对不起,我真的是新手。

代码如下:

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

谢谢。

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

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");(否则它会占用我的系统名称并给出错误(,并且此设置有效。

最新更新