`

直接使用java来调用mail.jar的API应用实例

    博客分类:
  • java
阅读更多
直接使用java来调用mail.jar的API应用实例
/** 修改历史
*   日期               作者          修改内容
* -----------------------------------------------------------------------------
* 2008-11-10    李小强         创建CLASS
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/**
* <p>Title:领头鸟科技全球信息咨询服务与解决方案提供商自主研发产品,直接使用java来调用mail.jar的API应用实例</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: 领头鸟科技全球信息咨询服务与解决方案提供商</p>
* @author 李小强
* <p>author E-Mail: lsl-120@163.com
* <p>http://leaderbird.blogcn.com
* <p>@version 2.1</p>
*
*/
public class JavaMailTest {
  public static void main (String args[]) throws Exception {
     
      String host = "mail.163.com";   //发件人使用发邮件的电子信箱服务器
      String from = "aaa@163.com";    //发邮件的出发地(发件人的信箱)
      String to = "lsl-120@163.com";   //发邮件的目的地(收件人信箱), Get system properties
      Properties props = System.getProperties(); // Setup mail server
      props.put("mail.smtp.host", host); // Get session
      props.put("mail.smtp.auth", "true"); //这样才能通过验证
      MyAuthenticator myauth = new MyAuthenticator("myname", "mypwd");
      Session session = Session.getDefaultInstance(props, myauth);
//      session.setDebug(true);
      // Define message
      MimeMessage message = new MimeMessage(session);
      // Set the from address
      message.setFrom(new InternetAddress(from));
      // Set the to address
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      // Set the subject
      message.setSubject("测试程序!");
      // Set the content
      message.setText("这是用java写的发送电子邮件的测试程序!");
      message.saveChanges();
      Transport.send(message);     
    }
  }

  //校验发信人权限的方法
//  package com.hyq.test;
//
//  import javax.mail.PasswordAuthentication;

  class MyAuthenticator extends javax.mail.Authenticator {
      private String strUser;
      private String strPwd;
      public MyAuthenticator(String user, String password) {
        this.strUser = user;
        this.strPwd = password;
      }
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(strUser, strPwd);
      }
    }


分享到:
评论
2 楼 leaderbird 2010-05-11  
[size=x-large][size=x-small]Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: mail.163.com, port: 25;
  nested exception is:
java.net.ConnectException: Connection refused: connect

这个是说:对方的服务器拒绝连接,我这个实例是在08年时使用的.有可能是163把它的pop3或smtp服务给关闭了。有可能是需要收费了!
1 楼 开拓者java 2010-05-04  
李总,在我的电脑上,为什么无法执行呢?提示如下:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: mail.163.com, port: 25;
  nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at guanChao.javaMailTest.main(javaMailTest.java:44)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more
我只改了用户名和密码。其他未变。

相关推荐

Global site tag (gtag.js) - Google Analytics