我目前正在处理忘记密码功能,我正在使用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());
}
}
非常感谢。
答案 0 :(得分:1)
我在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");
(否则它将使用我的系统名称并给出了错误),并且此设置有效。