我正在用java构建一个电子邮件应用程序,到目前为止我尝试使用它,我在运行此代码时没有收到任何错误,但是我编译并运行代码后的问题是net beans IDE只是向我显示,程序正在运行,但是当我检查邮箱时,没有收到这样的邮件。谁能解释我为什么会这样?
我还在最后打印了一条“成功发送”消息,但不知何故该消息没有被打印,我无法弄清楚错误是什么,任何帮助将不胜感激,谢谢。
public class Email
public static void main(String[] args) {
// TODO code application logic here
String[] to = {"pqr@gmail.com"};
String from = "abc@gmail.com";
String host = "smtp.gmail.com";
String user_name = "abc@gmail.com";
String password = "xyz";
try{
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", "imaps");
properties.put("smtp.starttls.enable", "true");
properties.put("mail.smtp.host",host);
properties.put("mail.smtp.user",user_name);
properties.put("mail.smtp.password", password);
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
//properties.put
Session session = Session.getDefaultInstance(properties,null);
//Store store = session.getStore("imaps");
MimeMessage message = new MimeMessage(session);
// basically stores one or more addresses , whom the mail has to be sent
//InternetAddress[] send_to = { new InternetAddress(to) };
//InternetAddress[] send_from = { new InternetAddress(from) };
InternetAddress[] toAddress = new InternetAddress[to.length];
for(int i = 0; i < to.length ;i++)
{
toAddress[i] = new InternetAddress(to[i]);
}
for(int i = 0 ; i < toAddress.length ; i++){
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setFrom(new InternetAddress(from));
message.setSubject("Testing email");
message.setText("this is a test");
Transport transport = session.getTransport("smtp");
transport.connect(host, user_name, password);
transport.send(message);
transport.close();
System.out.println("Message successfully sent");
}catch(Exception e){
e.printStackTrace();
}
}
}