カレンダーに javaxmail を添付して電子メールを送信したいので、次のクラスを作成しました。
public void sendEmail(String to, Calendar calendar) {
try {
String d_uname = "myaccount@gmail.com";
String d_password = "mypassword";
String d_host = "smtp.gmail.com";
String d_port = "587";//465,587
String from = "antonitocea@gmail.com";
String subject = "Subject";
String bodyText = "Body";
Properties prop = new Properties();
//prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.host", "smtp.gmail.com");
prop.setProperty("mail.smtp.protocol", "smtps");
prop.setProperty("mail.smtp.starttls.enable", "true");
prop.setProperty("mail.smtp.ssl.enable", "true");
prop.setProperty("mail.smtp.port",d_port);
prop.setProperty("mail.smtp.user", d_uname);
Session session = Session.getDefaultInstance(prop, null);
// Define message
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.addHeaderLine("method=REQUEST");
message.addHeaderLine("charset=UTF-8");
message.addHeaderLine("component=VEVENT");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Outlook Meeting Request Using JavaMail");
StringBuffer sb = new StringBuffer();
StringBuffer buffer = sb.append(calendar.toString());
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
messageBodyPart.setHeader("Content-ID","calendar_message");
messageBodyPart.setDataHandler(new DataHandler(
new ByteArrayDataSource(buffer.toString(), "text/calendar")));//very important
// Create a Multipart
Multipart multipart = new MimeMultipart();
// Add part one
multipart.addBodyPart(messageBodyPart);
Transport t = session.getTransport("smtp");
t.connect(d_host, 587, d_uname, d_password);
// Put parts in message
message.setContent(multipart);
// send message
t.send(message);
} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
問題ないようですが、送信しようとすると、コンソールに次のエラーが表示されます。
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
java.net.SocketException: Permission denied: connect
誰かが私を助けることができますか?前もって感謝します!!