无法在Linux中使用JavaMail API发送电子邮件 [英] Unable to send email using javamail api in linux
问题描述
这是我的代码,它可以在Windows机器上运行,但不能在linux上运行... 它不会抛出任何异常..在linux上
Here is my code it works on Windows machine but it does not work in linux... its not throwing any exception.. on linux
public void alert(String recipient, String subject , String error){
final String username = customize.getString("alertSenderEmail");
final String password = customize.getString("alertSenderPassword");
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.host", "smtp1.qualitykiosk.com");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
String[] receivers = recipient.split(",");
for (String receiver : receivers) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver));
message.setSubject(subject);
message.setText(error);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
//throw new RuntimeException(e);
}
}
}
错误-:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp1.qualitykiosk.com", port 25, isSSL false
220 ESMTP ESMTP
DEBUG SMTP: connected to host "smtp1.qualitykiosk.com", port: 25
EHLO
501 Syntax: EHLO hostname
HELO
501 Syntax: HELO hostname
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp1.qualitykiosk.com", port 25, isSSL false
220 ESMTP ESMTP
DEBUG SMTP: connected to host "smtp1.qualitykiosk.com", port: 25
EHLO
501 Syntax: EHLO hostname
HELO
501 Syntax: HELO hostname
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp1.qualitykiosk.com", port 25, isSSL false
220 ESMTP ESMTP
DEBUG SMTP: connected to host "smtp1.qualitykiosk.com", port: 25
EHLO
501 Syntax: EHLO hostname
HELO
501 Syntax: HELO hostname
推荐答案
如果Java/JavaMail无法检测到您当前的主机名,则可能发生这种情况.使用属性mail.smtp.localhost
显式指定EHLO
/HELO
的主机名.
This can happen if Java/JavaMail cannot detect your current hostname. Use property mail.smtp.localhost
to explicitly specify the hostname for EHLO
/HELO
.
来自 https://javamail .java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html :
mail.smtp.localhost
String
SMTP HELO或EHLO命令中使用的本地主机名.默认为InetAddress.getLocalHost().getHostName()
.如果您的JDK和名称服务配置正确,通常不需要设置.
mail.smtp.localhost
String
Local host name used in the SMTP HELO or EHLO command. Defaults toInetAddress.getLocalHost().getHostName()
. Should not normally need to be set if your JDK and your name service are configured properly.
作为更长期的解决方案,您可能想要找出InetAddress.getLocalHost().getHostName()
为什么(显然)没有返回主机名的原因.
As a more long term solution, you might want to find out why InetAddress.getLocalHost().getHostName()
is (apparently) not returning a hostname.
这篇关于无法在Linux中使用JavaMail API发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!