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
アクセス許可の拒否が何を意味するのかわかりません。ウイルス対策がブロックしているためか、このようなことが原因である可能性がありますか?
助けてください!