【发布时间】:2016-08-23 12:20:09
【问题描述】:
我有一个名为“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 并能够使用当前配置发送电子邮件?
谢谢。
【问题讨论】:
标签: java proxy connection jakarta-mail ports