[收藏] 使用javamail发信过程中的一些问题及解决方法

[收藏] 使用javamail发信过程中的一些问题及解决方法

  1  今天在研究javamail发信的过程中,出现了一些小问题,现总结如下,以免后来者走些不必要的弯路,先把完整的能够正常运行的代码示例粘贴如下:
  2  发邮件源代码:
  3  package  com.hyq.test;
  4 
  5  import  java.util.Properties;
  6  import  javax.mail. * ;
  7  import  javax.mail.internet. * ;
  8 
  9  public   class  MailExample {
 10 
 11     public   static   void  main (String args[])  throws  Exception {
 12      
 13      String host  =   " smtp.163.com " ;    // 发件人使用发邮件的电子信箱服务器
 14      String from  =   " 你自己的电子信箱 " ;     // 发邮件的出发地(发件人的信箱)
 15      String to  =   " 收件人信箱 " ;    // 发邮件的目的地(收件人信箱)
 16 
 17       //  Get system properties
 18      Properties props  =  System.getProperties();
 19 
 20       //  Setup mail server
 21      props.put( " mail.smtp.host " , host);
 22 
 23       //  Get session
 24      props.put( " mail.smtp.auth " " true " );  // 这样才能通过验证
 25 
 26      MyAuthenticator myauth  =   new  MyAuthenticator( " 你自己的电子信箱 " " 你自己的信箱密码 " );
 27      Session session  =  Session.getDefaultInstance(props, myauth);
 28 
 29  //     session.setDebug(true);
 30 
 31       //  Define message
 32      MimeMessage message  =   new  MimeMessage(session);
 33      
 34 
 35       //  Set the from address
 36      message.setFrom( new  InternetAddress(from));
 37 
 38       //  Set the to address
 39      message.addRecipient(Message.RecipientType.TO,
 40         new  InternetAddress(to));
 41 
 42       //  Set the subject
 43      message.setSubject( " 测试程序! " );
 44 
 45       //  Set the content
 46      message.setText( " 这是用java写的发送电子邮件的测试程序! " );
 47 
 48      message.saveChanges();
 49 
 50        Transport.send(message);
 51      
 52    }
 53  }
 54 
 55  校验发信人权限的方法
 56  package  com.hyq.test;
 57 
 58  import  javax.mail.PasswordAuthentication;
 59 
 60  class  MyAuthenticator
 61         extends  javax.mail.Authenticator {
 62       private  String strUser;
 63       private  String strPwd;
 64       public  MyAuthenticator(String user, String password) {
 65         this .strUser  =  user;
 66         this .strPwd  =  password;
 67      }
 68 
 69       protected  PasswordAuthentication getPasswordAuthentication() {
 70         return   new  PasswordAuthentication(strUser, strPwd);
 71      }
 72    }
 73 
 74 
 75  注意:上面的事例仅为使用163信箱时发送电子邮件的方法,因为使用的host为:smtp. 163 .com,如源代码中:String host  =    " smtp.163.com " ;    // 发件人使用发邮件的电子信箱服务器,如果使用其它的电子邮件发送,就必须在其邮件服务器上查找相应的电子邮件服务器,例如搜狐就要使用smtp.sohu.com,具体情况具体对待,都可以从所使用的邮件服务器上获得的。如果没有使用host ,也就是说,没有进行props.put("mail.smtp.host", host);设置,那么就会抛 javax.mail.MessagingException: Could not connect to SMTP host: localhost,  port: 25;的异常。当然了,如果你没有正确配置,这个异常仍然会被抛出的。
 76 
 77  有些邮件服务系统是不需要验证发件人的授权的,所以可以很简单的使用
 78      Session session  =  Session.getDefaultInstance(props,  null );
 79               而不必使用
 80      props.put( " mail.smtp.auth " " true " ); 
 81      MyAuthenticator myauth  =   new  MyAuthenticator( " 你自己的电子信箱 " " 你自己的信箱密码 " );
 82      Session session  =  Session.getDefaultInstance(props, myauth);
 83 
 84  就可以发送电子邮件了,这个多为一些企事业单位的内部电子信箱系统。
 85  但是对于很多门户网站上的电邮系统,如: 163 ,sohu,yahoo等等,如果仍然简单的这样使用就会抛
 86 
 87  com.sun.mail.smtp.SMTPSendFailedException:  553  authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA == .32705S2
 88 
 89 
 90   at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java: 1388 )
 91 
 92   at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java: 959 )
 93 
 94   at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java: 583 )
 95 
 96   at javax.mail.Transport.send0(Transport.java: 169 )
 97 
 98   at javax.mail.Transport.send(Transport.java: 98 )
 99 
100  这样的异常,要求你必须进行授权校验,它的目的就是阻止他人任意乱发邮件,也算是为了减少垃圾邮件的出现吧。这时候,我们就要使用
101      props.put( " mail.smtp.auth " " true " ); 
102      MyAuthenticator myauth  =   new  MyAuthenticator( " 你自己的电子信箱 " " 你自己的信箱密码 " );
103      Session session  =  Session.getDefaultInstance(props, myauth);
104 
105  这里还有一个特别注意的事情:在你使用Session.getDefaultInstance时,一定要将    props.put ( " mail.smtp.auth " " true " ); 置为true,它默认的是false,如果你没有做这一步,虽然你使用了 Session.getDefaultInstance(props, myauth);,你自己也确实     MyAuthenticator myauth  =   new  MyAuthenticator( " 你自己的电子信箱 " " 你自己的信箱密码  " );但是它仍然会抛出
106  com.sun.mail.smtp.SMTPSendFailedException:  553  authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA == .40815S2
107 
108 
109   at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java: 1388 )
110 
111   at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java: 959 )
112 
113   at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java: 583 )
114 
115   at javax.mail.Transport.send0(Transport.java: 169 )
116 
117   at javax.mail.Transport.send(Transport.java: 98 )
118  这样的异常。我就在这一步费了好长时间,后来才发现了这个问题,很是郁闷。不过还好,总算解决了。
119 
120  其实上面的做法只是比较简单的一种,也有很多其它的写法,如:
121  Properties props  =  System.getProperties();可以使用
122  Properties props  =   new  Properties();来代替。
123 
124 
125  Transport.send(message);可以使用下面的代码来代替
126        String username  =   " 你的电子信箱用户名 " ;
127        String password  =   " 你的电子信箱密码 " ;
128        message.saveChanges();  //     implicit with send()
129        Transport transport  =  session.getTransport( " smtp " );
130        transport.connect( " mail.htf.com.cn " , username, password);
131        transport.sendMessage(message, message.getAllRecipients());
132        transport.close();
133  这种方法在同时发送多封电子邮件时比较有用。
134 
135  还有一些具体的相关概念,可以查看相关的官方文档,在我查询资料时,发现了一篇文章写得相当仔细,可以加以参考:http: // www.matrix.org.cn/resource/article/44/44101_JavaMail.html
136 
137  另附上使用org.apache.commons.mail进行发电子邮件的示例:
138  import  org.apache.commons.mail.SimpleEmail;
139  import  org.apache.commons.mail. * ;
140 
141  public   class  TestCommon {
142     public  TestCommon() {
143    }
144     public   static   void  main(String[] args){
145      SimpleEmail email  =   new  SimpleEmail();
146      email.setHostName( " smtp.163.com " ); // 设置使用发电子邮件的邮件服务器
147       try  {
148        email.addTo( " 收件人信箱 " );
149        email.setAuthentication( " 发件人信箱 " , " 发件人信箱密码 " );
150        email.setFrom( " 发件人信箱 " );
151        email.setSubject( " Test apache.commons.mail message " );
152        email.setMsg( " This is a simple test of commons-email " );
153        email.send();
154      }
155       catch  (EmailException ex) {
156        ex.printStackTrace();
157      }
158    }
159 
注:文章来自http://garyea.javaeye.com/blog/76460

你可能感兴趣的:([收藏] 使用javamail发信过程中的一些问题及解决方法)