在eclipse中使用Java Mail发送邮件时出错

时间:2015-07-12 09:13:11

标签: java javamail

我尝试在Eclipse中使用Java发送一个简单的邮件。当我试图运行该程序时,一个名为ast jtree的小窗口打开了。它有root后跟c1 c2 c3并且邮件没有发送。

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SingleEmail {

    public static void main(String[] args) {
         String to = "thamaraikani8@gmail.com";
          String from = "buddycoolrj@gmail.com"; 
          String host = "localhost";//or IP address  

         //Get the session object  
          Properties properties = System.getProperties(); 
          properties.setProperty("mail.user", "buddycoolrj@gmail.com");
          properties.setProperty("mail.password", "tecknodel");
          properties.setProperty("mail.smtp.host", host);  
          Session session = Session.getDefaultInstance(properties);  
         //compose the message  
          try{  
             MimeMessage message = new MimeMessage(session);  
             message.setFrom(new InternetAddress(from));  
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
             message.setSubject("Ping");  
             message.setText("Hello, this is example of sending email  ");  

             // Send message  
             Transport.send(message);  
             System.out.println("message sent successfully....");  

          }catch (MessagingException mex) {mex.printStackTrace();}  
    }

}

1 个答案:

答案 0 :(得分:1)

因为您使用gmail ID来自&要解决,您可以使用以下gmail服务器详细信息发送电子邮件。

props.put("mail.smtp.host", "smtp.gmail.com");  
props.put("mail.smtp.socketFactory.port", "465");  
props.put("mail.smtp.socketFactory.class",  
        "javax.net.ssl.SSLSocketFactory");  
props.put("mail.smtp.auth", "true");  
props.put("mail.smtp.port", "465");