不多说,直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
      * 发送邮件
      * @param subject 主题
      * @param to 收件人
      * @param username 邮箱账号
      * @param password 邮箱密码
      * @param host 邮件服务器
      * @param content 邮件内容
      * @param isAuth 是否验证密码
      */
public  void  send(String subject, String to,String username, String password, String host, String content, boolean  isAuth) {
         try {
             String[] mailArray = to.split( ";" );
             //多个收件人
             InternetAddress[] addresses =  new  InternetAddress[mailArray.length];
             for ( int  i= 0 ;i<mailArray.length;i++){
                 addresses[i] =  new  InternetAddress(mailArray[i]);
             }
             
             //创建properties,用于生成发送邮件的session
             Properties properties =  new  Properties();
             properties.put( "mail.smtp.host" , host);    //发送邮件的主机
             properties.put( "mail.smtp.auth" , isAuth);  //是否验证发送人密码
             properties.put( "mail.smtp.port" 25 );      //端口
             
             //通过配置文件,生成一个session
             Session mailSession = Session.getInstance(properties, null );  
             mailSession.setDebug( true );
             
             //通过session开始创建message
             MimeMessage mailMessage =  new  MimeMessage(mailSession);  
             ////设置发件人,使用InternetAddress
             mailMessage.setFrom( new  InternetAddress(username));   
              //收件人,这里为多个
             mailMessage.setRecipients(MimeMessage.RecipientType.TO, addresses);  
             mailMessage.setSubject(subject);   //邮件主题
                          
                         //Multipart用来防止邮件内容,其中可放多个BodyPart 
             Multipart multipart =  new  MimeMultipart();  
                          //第一个bodypart,放置普通文字吧
              BodyPart contentPart =  new  MimeBodyPart();
                      contentPart.setContent(content,  "text/html;charset=UTF-8" );
                      multipart.addBodyPart(contentPart);
                     //第二个bodypart,放置附件吧
                     BodyPart attachmentBodyPart =  new  MimeBodyPart();
                     //附件文件,需要用DataSource来构造对象
                         DataSource source =  new  FileDataSource( "E:/students.xls" );
                         //DataSource需要用DataHandler来构造
                         attachmentBodyPart.setDataHandler( new  DataHandler(source));
                         attachmentBodyPart.setFileName( "students.xls" ); //附件文件名
                         multipart.addBodyPart(attachmentBodyPart);
              
                         mailMessage.setContent(multipart);   //将内容设置给message
             mailMessage.setSentDate( new  Date());  //发送时间
             mailMessage.saveChanges();
             if (isAuth){
                 Transport.send(mailMessage, username, password);
             } else {
                 Transport.send(mailMessage);   //Transport开始发送邮件
             }
         } catch (Exception e){
             try  {
                 throw  e;
             catch  (Exception e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
             }
             e.printStackTrace();
         }
     }
    

简单的写一个main方法

1
2
3
4
5
public  static  void  main(String arg[]) {
         Mail mail =  new  Mail();
         //主题,收件人,发件人,密码,邮件服务器。。。。。。。
         mail.send( "测试1" "jun.xie@chinacache.com" ,   "ren.shi@chinacache.com" "123456" "mail.chinacache.com" "testdfdf" false );
     }