我尝试使用 JavaMail 发送电子邮件,但收到异常,我不知道发生了什么!
public class Mail
{
private int port = 587;
private String host = "smtp.live.com";
private String from = "myusername@live.com";
private boolean auth = true;
private String username = "myusername@live.com";
private String password = "mypassword";
private Protocol protocol = Protocol.SMTPS;
private boolean debug = true;
@SuppressWarnings("incomplete-switch")
public void sendMail(String to, String subject, String body)
{
Properties outlookProps = new Properties();
outlookProps.put("mail.smtp.host", host);
outlookProps.put("mail.smtp.port", port);
switch (protocol) {
case SMTPS:
outlookProps.put("mail.smtp.ssl.enable", true);
break;
case TLS:
outlookProps.put("mail.smtp.starttls.enable", true);
break;
}
Authenticator authenticator = null;
if (auth) {
outlookProps.put("mail.smtp.auth", true);
authenticator = new outlookAuthenticator();
}
Session session = Session.getInstance(outlookProps, authenticator);
session.setDebug(debug);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddresses = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, toAddresses);
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
class outlookAuthenticator extends Authenticator
{
private PasswordAuthentication pa = new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
我收到了这个例外:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.live.com, 587; timeout -1;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at net.Mail.sendMail(Mail.java:54)
at ahmad_Project.Main.main(Main.java:13)
Caused by: java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 8 more
我不知道权限被拒绝是什么意思,可能是因为我的防病毒软件阻止了它或类似的东西吗?
请帮忙!