Java mail使用gmail作为主机,但不使用godaddy
我目前正在开发忘记密码功能,我正在使用javax.mail。我尝试了Java mail使用gmail作为主机,但不使用godaddy,java,Java,我目前正在开发忘记密码功能,我正在使用javax.mail。我尝试了smtp.gmail.com作为我的主机,它在本地和实时服务器上运行良好。在下面的链接中,你可以看到一个由gmail发送并签名的 但是,使用smtpout.secureserver.net只能在本地服务器上工作 在live server中,它表示无法连接到SMTP主机:smtpout.secureserver.net,端口:465,响应:554。而且,即使在本地服务器上查看电子邮件时,我也看不到签名者和邮寄者 我真的需要从Go
smtp.gmail.com
作为我的主机,它在本地和实时服务器上运行良好。在下面的链接中,你可以看到一个由gmail发送并签名的
但是,使用smtpout.secureserver.net
只能在本地服务器上工作
在live server中,它表示无法连接到SMTP主机:smtpout.secureserver.net,端口:465,响应:554
。而且,即使在本地服务器上查看电子邮件时,我也看不到签名者和邮寄者
我真的需要从GoDaddy购买证书才能查看签名人和邮寄人的?另外,这可能是它无法在live server中工作的原因吗?对不起,我对这件事还不熟悉
代码如下:
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());
}
}
公共类邮件程序{
信息;
运输;;
属性道具;
私有字符串用户名=”my@email.com";
专用字符串密码=”password@123";
私有字符串协议=“smtp”;
私有字符串host=“smtpout.secureserver.net”;
专用字符串端口=“465”;
私有字符串socketFactoryClass=“javax.net.ssl.SSLSocketFactory”;
私有最终字符串
MAIL\u SMTP\u START\u TLS\u ENABLE=“MAIL.SMTP.starttls.ENABLE”,
MAIL\u SMTP\u AUTH=“MAIL.SMTP.AUTH”,
MAIL\u TRANSPORT\u PROTOCOL=“MAIL.TRANSPORT.PROTOCOL”,
MAIL\u DEBUG=“MAIL.DEBUG”,
MAIL\u SMTP\u PORT=“MAIL.SMTP.PORT”,
MAIL\u SMTP\u HOST=“MAIL.SMTP.HOST”,
MAIL\u SMTP\u SOCKETFACTORY\u PORT=“MAIL.SMTP.SOCKETFACTORY.PORT”,
MAIL\u SMTP\u SOCKETFACTORY\u CLASS=“MAIL.SMTP.SOCKETFACTORY.CLASS”,
MAIL\u SMTP\u SSL\u ENABLE=“MAIL.SMTP.SSL.ENABLE”;
公共邮递员(){
setProperties();
}
公共void setProperties(){
props=System.getProperties();
props.put(MAIL\u SMTP\u AUTH,true);
//props.put(邮件\u SMTP\u开始\u TLS\u启用,true);
props.put(MAIL\u DEBUG,true);
道具放置(邮件传输协议,协议);
props.put(邮件、SMTP、端口);
props.put(邮件、SMTP、主机);
props.put(邮件\u SMTP\u SSL\u启用,true);
//props.put(MAIL_SMTP_SOCKETFACTORY_PORT,String.valueOf(PORT));
//props.put(邮件、SMTP、SOCKETFACTORY、socketFactoryClass);
}
public void sendPasswordToEmail(字符串recipientEmail,用户用户)引发异常{
Session Session=Session.getInstance(props,null);
MimeMessage message=新MimeMessage(会话);
试一试{
message.setFrom(新的Internet地址(用户名));
message.addRecipients(message.RecipientType.TO,InternetAddress.parse(recipientEmail));
message.setSubject(“忘记密码请求”);
StringBuffer messageBuffer=新的StringBuffer();
append(“我的Web应用门户”);
append(“这是您的登录详细信息:
”;
messageBuffer.append(“用户名:“+user.getLoginUser()+”
”;
messageBuffer.append(“密码:+user.getPassword()+”
”;
message.setContent(messageBuffer.toString(),“text/html;charset=utf-8”);
message.setSentDate(新日期());
传输=session.getTransport(“smtp”);
传输连接(主机、用户名、密码);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}捕获(AuthenticationFailedException ex){
System.out.println(例如getMessage());
}
}
非常感谢。我在SpringBoot中的配置(将domainname.com替换为您的域名和密码)
我必须添加mimeMessageHelper.setFrom(“info@domainname.com“”;
在发送邮件之前(否则它会使用我的系统名并给出错误)这个设置很有效。我所看到的一切都表明,你需要一个GoDaddy帐户才能通过GoDaddy发送电子邮件。很抱歉反应太晚。是的。我已经有了GoDaddy帐户。谢谢。等等。GoDaddy电子邮件帐户与GoDaddy本身不同吗?我真的很抱歉。我想你需要GoDaddy电子邮件中继。如果你有dom,我想你会得到一些艾恩,你可以付更多的钱。但你真的应该问戈达迪支持这些问题!!
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: "*"