我下载了以下示例:
https://app.box.com/shared/etj0lts287r5lc1hqeex
当我直接执行jar文件时,它工作得很好。但是当使用Netbeans 7.0运行项目时,它抛出一个错误:
error: could not connect to SMTP host:smtp.gmail.com,port:465
有什么问题吗?
这是我的代码按钮动作执行私有void发送按钮动作执行(java.awt.event.ActionEvent evt){
boolean isSent = true;
try {
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", txtfrom.getText());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", txthostName.getText());
properties.put("mail.smtp.user", txtfrom.getText());
properties.put("mail.smtp.port", txtPort.getText());
properties.put("mail.smtp.socketFactory.port", txtPort.getText());
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
Authenticator mailAuthenticator = new MailAuthenticator();
Session mailSession = Session.getDefaultInstance(properties,mailAuthenticator);
Message message = new MimeMessage(mailSession);
Transport transport = mailSession.getTransport("smtps");
transport.connect("smtp.gmail.com",465,"abc@gmail.com","12345");
InternetAddress fromAddress = new InternetAddress(txtfrom.getText());
InternetAddress toAddress = new InternetAddress(txtto.getText());
message.setFrom(fromAddress);
message.setRecipient(RecipientType.TO, toAddress);
message.setSubject(txtsubject.getText());
message.setText(txtcontent.getText());
Transport.send(message);
} catch (Exception e) {
lblInfo.setText("ERROR:" + e.getMessage());
isSent = false;
}
if(isSent == true) {
sendButton.setEnabled(false);
lblInfo.setText("Your e-mail has been sent.");
}
}
2条答案
按热度按时间q9rjltbz1#
请考虑对您的代码进行以下更改,
//使用“smtps”:)
//端口465用于身份验证,并通过Gmail解决,Yahoo主机
transport.connect("smtp.gmail.com",465,"myid","password");
5cnsuln72#
JavaMail是发送电子邮件的传统答案。请注意
port
和其他配置是如何设置的。这里有一个例子来做这件事。
查看更多Java mail sending示例。