3
我已经实现了电子邮件类并从属性文件中获取所有属性。 这里是我的代码:如何在处理电子邮件服务器时处理代理服务器?
static {
// Load the properties file
try {
properties = new Properties();
InputStream inputStream = Email.class.getClassLoader()
.getResourceAsStream("/mail.properties");
properties.load(inputStream);`enter code here`
} catch (Exception e) {
logger.error(e.getMessage());
}
}
/**
*
* @param to
* : mail Sent to
* @param from
* : mail sent from
* @param subject
* : mail's subject
* @param body
* : mail's body
* @throws Exception
*/
public static void sendTextMail(String to, String from, String subject,
String body) throws Exception {
if (properties.isEmpty()) {
throw new Exception("Cannot send mail. Host data not available.");
}
// Authenticate the session with username and password
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication((String) properties
.get("mail.login.username"), (String) properties
.get("mail.login.password"));
}
});
// Create to and from addresses
InternetAddress fromAddress = new InternetAddress(from);
InternetAddress toAddress = new InternetAddress(to);
// Create the message instance
// and add the sender, recipient, subject and body.
Message msg = new MimeMessage(session);
msg.setFrom(fromAddress);
msg.setSubject(subject);
msg.setRecipient(RecipientType.TO, toAddress);
msg.setContent(body, "text/plain");
// Finally send the email
Transport.send(msg);
}
,当我试图发送邮件我得到这个错误: 16:48:23882 ERROR AuthorController:1199 - 无法连接到SMTP主机:smtp.gmail.com端口: 25 代理正在阻止我的服务器。如何解决此问题
尝试在端口465上使用TLS代替。由于垃圾邮件发送者,端口25通常被阻止。 – artbristol
你确定它是一个代理,而不仅仅是一个普通的防火墙? –