javamail 配置链接socket代理服务器发送邮件,报错SOCKS:authentication faild
发送主要代码如下
public boolean sendBatch(String sysName) {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", this.mailSMTPHost);
properties.setProperty("mail.smtp.port", StringUtils.isNotBlank(mailPort)?mailPort:"25");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.ssl.trust", this.mailSMTPHost);
properties.setProperty("mail.debug", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.setProperty("mail.host", this.mailSMTPHost);
properties.setProperty("mail.transport.protocol", "smtp");
logger.info("代理参数:"+this.proxyType + this.proxyHost + this.proxyPort);
if (StringUtils.equals(this.proxyType,"socket")) {
properties.setProperty("proxySet", "true");
properties.setProperty("socksProxyHost", this.proxyHost);
properties.setProperty("socksProxyPort", this.proxyPort);
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory", sf);
}else if (StringUtils.equals(this.proxyType,"http")) {
properties.setProperty("proxySet", "true");
properties.setProperty("http.proxyHost", this.proxyHost);
properties.setProperty("http.proxyPort", this.proxyPort);
}
logger.info("properties:"+ properties.toString());
MyAuthenticator myauth = new MyAuthenticator(this.mailUser, this.mailPassword);
Session session = Session.getDefaultInstance(properties, myauth);
Message mimemessage = new MimeMessage(session);
try {
String nick = MimeUtility.encodeText(sysName);
mimemessage.setFrom(new InternetAddress(nick + "<" + this.mailFromAddress + ">"));
mimemessage.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
} catch (AddressException e) {
logger.info("发送邮箱Address异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
} catch (MessagingException e) {
logger.info("发送邮箱Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
} catch (UnsupportedEncodingException e) {
logger.info("发送邮箱异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
try {
if (mailTOAddress != null) {
mimemessage.addRecipients(Message.RecipientType.TO, this.mailTOAddress);
}
if (mailCCAddress != null) {
mimemessage.addRecipients(Message.RecipientType.CC, this.mailCCAddress);
}
if (mailBCCAddress != null) {
mimemessage.addRecipients(Message.RecipientType.BCC, this.mailBCCAddress);
}
} catch (MessagingException e) {
logger.info("接受邮箱Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
try {
mimemessage.setSubject(this.mailSubject);
} catch (MessagingException e) {
logger.info("设置主题Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
try {
mimemessage.setContent(this.mailBody);
} catch (Exception e) {
logger.info("设置内容Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
try {
mimemessage.setSentDate(new Date());
} catch (MessagingException e) {
logger.info("设置发送日期Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
Transport transport;
try {
transport = session.getTransport();
transport.connect(this.mailSMTPHost,Integer.valueOf(this.mailPort), this.mailUser, this.mailPassword);
} catch (NoSuchProviderException e) {
logger.info("获取SMTP时NoSuchProvider异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
} catch (MessagingException e) {
logger.info("连接发送邮箱服务器Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
try {
Transport.send(mimemessage);
logger.info("邮件发送Messaging成功");
} catch (MessagingException e) {
logger.info("邮件发送Messaging异常!" + e.getMessage());
throw new RuntimeException("发送邮件失败!",e);
}
emailPrint();
return true;
}
配置文件如下
prop:
#公司环境
email:
fromAddress: 123@123.com
smtpHost: smtp.qiye.163.com
fromUser: 123@123.com
fromPassword: **********
smtpPort: 994
proxyType: socket
proxyHost: 00.00.00.00
proxyPort: 1080
但是socket代理服务器没有设置用户名密码,试过各种配置还是报错,请各位看一下啊!
你可以参考这篇文章:https://blog.csdn.net/u010687717/article/details/82658657
或者这篇:https://blog.csdn.net/weixin_40337982/article/details/111589140
如果不行建议你应该将你的xml配置文件发出来
你配置的邮箱信息是邮箱账号和密码吗?应该配置授权码而非密码哦,看看你是不是因为这个导致的