我下载了以下示例:
https://app.box.com/shared/etj0lts287r5lc1hqeex
当我直接执行jar文件时,它工作得很好。但是在使用Netbeans 7.0运行项目时,它会抛出一个错误:
error: could not connect to SMTP host:smtp.gmail.com,port:465
可能是什么问题?
这是我执行按钮操作的代码 private void sendButtonActionPerformed(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.");
}
}
答案 0 :(得分:0)
请考虑对您的代码进行以下更改
//使用“smtps”:)
transport = mailSession.getTransport("smtps");
//端口465用于身份验证,并通过gmail,yahoo hosts transport.connect("smtp.gmail.com",465,"myid","password");
答案 1 :(得分:0)
JavaMail是发送电子邮件的传统答案。
请注意如何设置port
和其他配置。
这里是一个例子。
package example.java.mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendFromGmail {
public static void main(String args[]){
try{
String host = "smtp.gmail.com";
String from = "example@gmail.com";
String pass = "mypassword123";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"example@gmail.com"};
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception e){
e.getMessage();
}
}
}
查看更多Java mail sending示例。