我有一个名为“NotifiationService”的类:@Service(“notificacionService”)公共类NotificacionServiceImpl实现NotificacionService {
//servicio llama a repositorio
@Autowired
@Qualifier("notificacionService")
private NotificacionService notificacionService;
@Override
public void send(String to, String subject, String text) {
//proxy
Properties p = System.getProperties();
p.setProperty("proxySet","true");
p.setProperty("socksProxyHost","MYPROXY");
p.setProperty("socksProxyPort","MYPORT");
Properties props = new Properties();
// smtp.gmail.com
props.setProperty("mail.smtp.host", "smtp.gmail.com");
// TLS
props.setProperty("mail.smtp.starttls.enable", "false");
// port
props.setProperty("mail.smtp.port","465");
//
props.setProperty("mail.smtp.user", "reciever@gmail.com");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("sender@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
//
Transport t = session.getTransport("smtp");
t.connect("sender@gmail.com","senderpassword");
t.sendMessage(message,message.getAllRecipients());
t.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正如您所看到的,我已尝试配置代理,因为计算机已连接到重定向流量的计算机。所以即使添加了关于代理的所有规范,它仍然给我一个错误说:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.SocketException: Permission denied: connect
我也尝试了不同的端口,如:25,485,587,没有响应,所以我认为这是代理问题。
为了能够找到有关已实现的代理的信息,我在控制台中输入了以下命令:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | find /i "proxyserver"
它响应:
ProxyServer REG_SZ MYPROXY:MYPORT
如果我在cmd中键入:“ping google.com”,则表示无法访问
有没有办法能够从java与javamail连接到Gmail,并能够发送当前配置的电子邮件?
谢谢。
如果在设置SOCKS属性后它无法正常工作,则很可能您的代理服务器只是一个Web代理服务器而不是SOCKS代理服务器。 JavaMail FAQ指向其他允许通过Web代理服务器进行隧道连接的软件。
从JavaMail 1.6.2开始,您可以为Session对象设置代理身份验证属性以发送电子邮件。
请参阅以下文档链接。 https://javaee.github.io/javamail/docs/api/
新引入了以下属性,并且可以与代理身份验证(基本)一起使用。
买了.SMTP.proxy.host
mail.smtp.proxy.port
mail.smtp.proxy.user
mail.smtp.proxy.password