好吧,我会说尝试连接时存在身份验证问题。您不提供任何用户名或密码,除非您的交换服务器不需要用户名和密码。
更新:如果使用JDK 7,请参阅以下文章,它解决了此问题:
缺陷-使用VPN时,套接字拒绝JDK7权限
“更多地挖掘,似乎VPN客户端已禁用IPv6,这导致JDK7出现问题。如果我使用以下标志-Djava.net.preferIPv4Stack =
true,我将不再看到错误。这是预期的解决方法还是这是一个问题?
public class MailTest { public static void main(String[] args) throws MessagingException { String host = "smtp.gmail.com"; String to = "myEmail@gmail.com"; String from = "myEmail@gmail.com"; String subject = "test"; String messageText = "body test"; Properties props = System.getProperties(); props.put("mail.host", host); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", "25"); // If using authentication, otherwise comment out props.put("mail.smtp.auth", "true"); // Gmail requires TLS, your server may not props.put("mail.smtp.starttls.enable", "true"); Session mailSession = Session.getDefaultInstance(props, null); Message msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); msg.setSentDate(new Date()); msg.setText(messageText); Transport transport = mailSession.getTransport("smtp"); //connect with authentication //transport.connect(host,"myUsername" , "myPassword"); //connect without authentication transport.connect(); transport.sendMessage(msg, address); transport.close(); System.out.println("Mail was sent to " + to); System.out.println(" from " + from); System.out.println(" using host " + host + "."); }}


