[英]Send Email Without authentication in java using gmail smtp server and javamail
[英]Unable to send mails using smtp server-javamail
我一直在尝试通过javamail api使用smtp服务器发送电子邮件。 我收到以下错误:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.host.com, port: 587;
nested exception is:java.net.SocketException: Permission denied: connect
这是我为邮件创建“ Properties
和Session
对象的方法:
private Session getSession() {
Authenticator authenticator = new Authenticator();
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.smtp.host", "smtp.hostname.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.ssl.trust", "smtp.hostname.com");
properties.setProperty("java.net.preferIPv4Stack" , "true");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "username";
String password = "password";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
我尝试按照某些帖子的建议禁用身份验证,并根据该帖子提出了针对IPv4首选项的解决方案: JavaMail API到iMail-java.net.SocketException:权限被拒绝:connect
但我仍然遇到相同的错误。 还有其他方法可以解决此问题吗?
谢谢。
我在这里发现了类似的问题。
尝试查看问题是否与防病毒或防火墙阻止您的请求有关。
关闭防火墙,并在Google中找到一些jar / lib / security中缺少的安全证书,我已经遇到过这个问题,然后我必须在php中使用phpmailer lib并在代码中的任何地方使用API以便可以尝试php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.