新年快乐!
我正在开发一个应用程序,用户只要发生特定触发器就会收到电子邮件.
这是我用来发送电子邮件的功能:
public static void sendEmail(String host,String port,String useSSL,String useTLS,String useAuth,String user,String password,String subject,String content,String type,String recipients)
throws NoSuchProviderException,AddressException,MessagingException {
final Properties props = new Properties();
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.smtp.host",host);
props.setProperty("mail.smtp.port",port);
if (useSSL != null && !useSSL.equals("false") && useSSL.equals("true")) {
props.setProperty("mail.smtp.ssl.enable",useSSL);
props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.port",port);
}
if (useTLS != null && !useTLS.equals("false") && useTLS.equals("true")) {
props.setProperty("mail.smtp.starttls.enable",useTLS);
props.setProperty("mail.smtp.socketFactory.fallback","true");
}
props.setProperty("mail.smtp.auth",useAuth);
props.setProperty("mail.from",user);
props.setProperty("mail.smtp.user",user);
props.setProperty("mail.password",password);
Session mailSession = Session.getDefaultInstance(props,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(props.getProperty("mail.smtp.user"),props
.getProperty("mail.password"));
}
});
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setHeader("Subject",subject);
message.setContent(content,type);
StringTokenizer tokenizer = new StringTokenizer(recipients,";");
while (tokenizer.hasMoreTokens()) {
String recipient = tokenizer.nextToken();
message.addRecipient(Message.RecipientType.TO,new InternetAddress(recipient));
}
transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
奇怪的是,每当我尝试使用main方法运行上述代码时,它都会成功发送SSL和TLS协议的电子邮件.
public static void main(String args[])
{
try {
Notifier.sendEmail("smtp.gmail.com","587","false","true","sender_email@gmail.com","testpassword","CHECKING SETTINGS","CHECKING EMAIL FUNCTIONALITY","text/html","cc_email@gmail.com");
} catch (Exception ex) {
ex.printStackTrace();
}
}
但每当我尝试通过我的Web应用程序运行相同的代码时它就会失败.
通过SSL发送它会引发此错误:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at
jvm 1 | 530 5.5.1 https://support.google.com/mail/answer/14257 f12sm88286300pat.20 - gsmtp
jvm 1 |
jvm 1 | at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
通过TLS发送会抛出此错误:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com,port: 587;
jvm 1 | nested exception is:
jvm 1 | javax.net.ssl.SSLException: Unrecognized SSL message,plaintext connection?
jvm 1 | at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
任何形式的帮助表示赞赏.
EDIT1:
这是前端的tpl文件
值将保存在配置文件中,如下所示:
host=smtp.gmail.com
port=587
ssl=false
tls=true
auth=true
user=send_user_email@gmail.com
password=O0UbYboDfVFRaiA=
recipients=cc_user_email@gmail.com
trigger1=false
attempt=0
trigger2=false
percent=5
anyOrAll=ANY
trigger3=true
format=HTML
trigger4=true
trigger5=true
EDIT2:
public static void sendEmail(String message)
throws NoSuchProviderException,MessagingException
{
if (message == null || message.trim().equals("")) return;
StringBuffer content = new StringBuffer();
content.append(getHeader());
content.append(message);
content.append(getFooter());
String format = NotifyProps.getFormat();
String type = "text/plain";
if (format.equals(NotifyProps.HTML)) type = "text/html";
sendEmail(NotifyProps.getHost(),NotifyProps.getPort(),Boolean.toString(NotifyProps.getUseAuth()),Boolean.toString(NotifyProps.getUseSSL()),Boolean.toString(NotifyProps.getUseTLS()),NotifyProps.getUser(),NotifyProps.getPassword(),"Transaction Processor Auto Notification",content.toString(),type,NotifyProps.getRecipients())
}
这是设置和获取属性的类:
https://codeshare.io/5G8ki
谢谢.
最佳答案
你的代码看起来还不错,至少有一个大问题.您正尝试对TLS和SSL使用相同的端口(587).我不确定TLS,但如果你将请求发送到端口465,SSL代码应该工作.正如写的here (google FAQ):
configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS)[…]
他们有自己的特定端口.您获得的SSL错误:
Unrecognized SSL message,plaintext connection?
您的客户端是否了解它收到非SSL编码响应的事实(由于TLS端口未实现SSL).
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!