2015-07-12 57 views
-2

我试图在Eclipse中使用Java发送简单邮件。当我试图运行该程序时,打开一个名为ast jtree示例的小窗口。它的根目录是c1 c2 c3,邮件没有发送。在eclipse中使用Java Mail发送邮件时出错

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

public class SingleEmail { 

    public static void main(String[] args) { 
     String to = "[email protected]"; 
      String from = "[email protected]"; 
      String host = "localhost";//or IP address 

     //Get the session object 
      Properties properties = System.getProperties(); 
      properties.setProperty("mail.user", "[email protected]"); 
      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

你有本地主机上运行的邮件服务器吗?因为这是你发送邮件的地方。 – Glorfindel

+0

其实我并不熟悉java,所以请你介绍一下邮件服务器。 – kani

+0

检查@马努的答案下面,那些似乎是正确的配置。 – Glorfindel

回答

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"); 
+0

我改变了代码如下。还存在同样的问题。 – kani

+0

尝试更改端口为587. – connorp

+0

我试过了,仍然存在问题。 – kani