 //SendMail.java
//SendMail.java
				
						
						
								 import
						 javax.mail.
						*
				
				
						;
						
				
				
						import
						 javax.mail.
						*
				
				
						;
 import
						 javax.mail.internet.
						*
				
				
						;
						import
						 javax.mail.internet.
						*
				
				
						;
 import
						 java.util.
						*
				
				
						;
						import
						 java.util.
						*
				
				
						;
 import
						 javax.activation.
						*
				
				
						;
						import
						 javax.activation.
						*
				
				
						;


 public
						 
						class
						 SendMail
						public
						 
						class
						 SendMail 
						
								 {
						
				
				
						
								{
 
    

 public
								 
								static
								 
								void
								 send(String customMailBoxAddress,String username,String password,String serverMailBoxAddress,String subject,String attachmentPath,String attachmentName)
    
								public
								 
								static
								 
								void
								 send(String customMailBoxAddress,String username,String password,String serverMailBoxAddress,String subject,String attachmentPath,String attachmentName) 
								
										 {
								
						
						
								
										{
 //
										这里面使用新浪作为发送邮件的邮件服务器,其他的smtp服务器可以到相关网站上查到。
        
										//
										这里面使用新浪作为发送邮件的邮件服务器,其他的smtp服务器可以到相关网站上查到。
								
								
										
										
												 String host 
										=
										 
										"
										smtp.sina.com.cn
										"
								
								
										;
										
								
								
										        String host 
										=
										 
										"
										smtp.sina.com.cn
										"
								
								
										;
 //
										发送方邮箱地址(如BlogJava2006@blog.com.cn.)
        
										//
										发送方邮箱地址(如BlogJava2006@blog.com.cn.)
								
								
										
										
												 String from 
										=
								
								
										 customMailBoxAddress;
										
								
								
										        String from 
										=
								
								
										 customMailBoxAddress;
 //
										收件人邮箱地址
        
										//
										收件人邮箱地址
								
								
										
										
												 String to 
										=
								
								
										 serverMailBoxAddress;
										
								
								
										        String to 
										=
								
								
										 serverMailBoxAddress;
 //
										发送者的邮箱用户名
        
										//
										发送者的邮箱用户名
								
								
										
										
												 String user 
										=
								
								
										 username;
										
								
								
										        String user 
										=
								
								
										 username;
 //
										发送者的邮箱密码
        
										//
										发送者的邮箱密码
								
								
										
										
												 String ps 
										=
								
								
										 password;
										
								
								
										        String ps 
										=
								
								
										 password;
 
        
 Properties props 
										=
										 
										new
								
								
										 Properties();
        Properties props 
										=
										 
										new
								
								
										 Properties();
 
        
 //
										设置发送邮件的邮件服务器的属性(这里使用新浪的smtp服务器)
        
										//
										设置发送邮件的邮件服务器的属性(这里使用新浪的smtp服务器)
								
								
										
										
												 props.put(
										"
										mail.smtp.host
										"
								
								
										, host);
										
								
								
										        props.put(
										"
										mail.smtp.host
										"
								
								
										, host);
 //
										需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有
										//
										这一条)
        
										//
										需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有
										//
										这一条)
								
								
										
										
												 props.put(
										"
										mail.smtp.auth
										"
										, 
										"
										true
										"
								
								
										);
										
								
								
										        props.put(
										"
										mail.smtp.auth
										"
										, 
										"
										true
										"
								
								
										);
 
        
 //
										用刚刚设置好的props对象构建一个session
        
										//
										用刚刚设置好的props对象构建一个session
								
								
										
										
												 Session session 
										=
								
								
										 Session.getDefaultInstance(props);
										
								
								
										        Session session 
										=
								
								
										 Session.getDefaultInstance(props);
 
        
 //
								
								
										有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
        
										//
								
								
										有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
 //
										用(有的时候网络连通性不够好,发送邮件可能会有延迟,在这里面会有所
										//
										提示,所以最好是加上这句,避免盲目的等待)
        
										//
										用(有的时候网络连通性不够好,发送邮件可能会有延迟,在这里面会有所
										//
										提示,所以最好是加上这句,避免盲目的等待)
								
								
										
										
												 session.setDebug(
										true
								
								
										);
										
								
								
										        session.setDebug(
										true
								
								
										);
 
        
 //
										定义消息对象
        
										//
										定义消息对象
								
								
										
										
												 MimeMessage message 
										=
										 
										new
								
								
										 MimeMessage(session);
										
								
								
										        MimeMessage message 
										=
										 
										new
								
								
										 MimeMessage(session);

 try
        
										try
										
												 {
										
								
								
										
												{
 message.setFrom(
												new
										
										
												 InternetAddress(from));
            message.setFrom(
												new
										
										
												 InternetAddress(from));
 message.addRecipient(Message.RecipientType.TO,
												new
										
										
												 InternetAddress(to));
            message.addRecipient(Message.RecipientType.TO,
												new
										
										
												 InternetAddress(to));
 message.setSubject(subject);
            message.setSubject(subject);
 
            
 //
												 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
            
												//
												 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
										
										
												
												
														 Multipart multipart 
												=
												 
												new
										
										
												 MimeMultipart();
												
										
										
												            Multipart multipart 
												=
												 
												new
										
										
												 MimeMultipart();
 //
												设置邮件的文本内容
            
												//
												设置邮件的文本内容
										
										
												
												
														 BodyPart contentPart 
												=
												 
												new
										
										
												 MimeBodyPart();
												
										
										
												            BodyPart contentPart 
												=
												 
												new
										
										
												 MimeBodyPart();
 contentPart.setText(
												"
												邮件的具体内容在此
												"
										
										
												);
            contentPart.setText(
												"
												邮件的具体内容在此
												"
										
										
												);
 multipart. addBodyPart(contentPart);
            multipart. addBodyPart(contentPart);
 //
												添加附件
            
												//
												添加附件
										
										
												
												
														 BodyPart attachmentPart
												=
												 
												new
										
										
												 MimeBodyPart();
												
										
										
												            BodyPart attachmentPart
												=
												 
												new
										
										
												 MimeBodyPart();
 DataSource source 
												=
												 
												new
										
										
												 FileDataSource(attachmentPath);
            DataSource source 
												=
												 
												new
										
										
												 FileDataSource(attachmentPath);
 attachmentPart.setDataHandler(
												new
										
										
												 DataHandler(source));
            attachmentPart.setDataHandler(
												new
										
										
												 DataHandler(source));
 //
												注意:下面定义的enc对象用来处理中文附件名,否则名称是中文的附
												//
												件在邮箱里面显示的会是乱码,
            
												//
												注意:下面定义的enc对象用来处理中文附件名,否则名称是中文的附
												//
												件在邮箱里面显示的会是乱码,
										
										
												
												
														 sun.misc.BASE64Encoder enc 
												=
												 
												new
										
										
												 sun.misc.BASE64Encoder();
												
										
										
												            sun.misc.BASE64Encoder enc 
												=
												 
												new
										
										
												 sun.misc.BASE64Encoder();
 messageBodyPart.setFileName(
												"
												=?GBK?B?
												"
												+
												enc.encode(attachmentName.getBytes())
												+
												"
												?=
												"
										
										
												);
            messageBodyPart.setFileName(
												"
												=?GBK?B?
												"
												+
												enc.encode(attachmentName.getBytes())
												+
												"
												?=
												"
										
										
												);
 multipart.addBodyPart(messageBodyPart);
            multipart.addBodyPart(messageBodyPart);
 
            
 //
												将multipart对象放到message中
            
												//
												将multipart对象放到message中
										
										
												
												
														 message.setContent(multipart);
												
										
										
												            message.setContent(multipart);
 //
												发送邮件
            
												//
												发送邮件
										
										
												
												
														 message.saveChanges();
												
										
										
												            message.saveChanges();
 Transport transport 
												=
												 session.getTransport(
												"
												smtp
												"
										
										
												);
            Transport transport 
												=
												 session.getTransport(
												"
												smtp
												"
										
										
												);
 transport.connect(host, username, password);
            transport.connect(host, username, password);
 transport.sendMessage(message, message.getAllRecipients());
            transport.sendMessage(message, message.getAllRecipients());
 transport.close();
            transport.close();

 }
										
								
								
										catch
										(Exception e)
        }
										
								
								
										catch
										(Exception e)
										
												 {
										
								
								
										
												{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
										
								
								
										
										
												 }
    }
								
						
						
								
								
										 }
}
						
				
				  
  import
		
		
				 javax.mail.
				*
		
		
				;
 import
				 java.util.
				*
		
		
				;
				import
				 java.util.
				*
		
		
				;
 import
				 java.io.
				*
		
		
				;
				import
				 java.io.
				*
		
		
				;


 public
				 
				class
				 ReceiveMail
				public
				 
				class
				 ReceiveMail 
				
						 {
				
		
		
				
						{

 //
						处理任何一种邮件都需要的方法
    
						//
						处理任何一种邮件都需要的方法
				
				
						
						
								 
								 private
						 
						void
						 handle(Message msg) 
						throws
						 Exception
						
				
				
						    
						private
						 
						void
						 handle(Message msg) 
						throws
						 Exception 
						
								 {
						
				
				
						
								{
 System.out.println(
								"
								邮件主题:
								"
								 
								+
						
						
								 msg.getSubject());
        System.out.println(
								"
								邮件主题:
								"
								 
								+
						
						
								 msg.getSubject());
 System.out.println(
								"
								邮件作者:
								"
								 
								+
								 msg.getFrom()[
								0
						
						
								].toString());
        System.out.println(
								"
								邮件作者:
								"
								 
								+
								 msg.getFrom()[
								0
						
						
								].toString());
 System.out.println(
								"
								发送日期:
								"
								 
								+
						
						
								 msg.getSentDate());
        System.out.println(
								"
								发送日期:
								"
								 
								+
						
						
								 msg.getSentDate());
 }
    }
						
				
				
						
						
								 
								
								 //
						处理文本邮件
    
				
				
						//
						处理文本邮件
				
				
						
						
								 
								 private
						 
						void
						 handleText(Message msg) 
						throws
						 Exception
						
				
				
						    
						private
						 
						void
						 handleText(Message msg) 
						throws
						 Exception 
						
								 {
						
				
				
						
								{
 this
						
						
								.handle(msg);
        
								this
						
						
								.handle(msg);
 System.out.println(
								"
								邮件内容:
								"
								+
						
						
								msg.getContent());
        System.out.println(
								"
								邮件内容:
								"
								+
						
						
								msg.getContent());
 }
    }
						
				
				
						
						
								 
								
								 //
						处理Multipart邮件,包括了保存附件的功能
    
				
				
						//
						处理Multipart邮件,包括了保存附件的功能
				
				
						
						
								 
								 private
						 
						static
						 
						void
						 handleMultipart(Message msg) 
						throws
						 Exception
						
				
				
						    
						private
						 
						static
						 
						void
						 handleMultipart(Message msg) 
						throws
						 Exception 
						
								 {
						
				
				
						
								{
 String disposition;
        String disposition;
 BodyPart part;
        BodyPart part;

 Multipart mp 
								=
						
						
								 (Multipart) msg.getContent();
        Multipart mp 
								=
						
						
								 (Multipart) msg.getContent();
 //
								Miltipart的数量,用于除了多个part,比如多个附件
        
								//
								Miltipart的数量,用于除了多个part,比如多个附件
						
						
								
								
										 int
								 mpCount 
								=
						
						
								 mp.getCount();
								
						
						
								        
								int
								 mpCount 
								=
						
						
								 mp.getCount();

 for
								 (
								int
								 m 
								=
								 
								0
								; m 
								<
								 mpCount; m
								++
								)
        
								for
								 (
								int
								 m 
								=
								 
								0
								; m 
								<
								 mpCount; m
								++
								) 
								
										 {
								
						
						
								
										{
 this
								
								
										.handle(msg);
            
										this
								
								
										.handle(msg);
 part 
										=
								
								
										 mp.getBodyPart(m);
            part 
										=
								
								
										 mp.getBodyPart(m);
 disposition 
										=
								
								
										 part.getDisposition();
            disposition 
										=
								
								
										 part.getDisposition();
 //
										判断是否有附件
            
										//
										判断是否有附件
								
								
										
										
												 if
										 (disposition 
										!=
										 
										null
										 
										&&
								
								
										 disposition.equals(Part.ATTACHMENT))
										
								
								
										            
										if
										 (disposition 
										!=
										 
										null
										 
										&&
								
								
										 disposition.equals(Part.ATTACHMENT))

 
            
										
												 {
										
								
								
										
												{
 //
												这个方法负责保存附件
                
												//
												这个方法负责保存附件
										
										
												
												
														 saveAttach(part);
												
										
										
												                saveAttach(part);

 }
										
								
								
										 
										else
            }
										
								
								
										 
										else
										 
										
												 {
										
								
								
										
												{
 //
												不是附件,就只显示文本内容
                
												//
												不是附件,就只显示文本内容
										
										
												
												
														 System.out.println(part.getContent());
												
										
										
												                System.out.println(part.getContent());
 }
            }
										
								
								
										
										
												 }
        }
								
								
								
						
						
								
								
										 }
    }
						
						
						
				
				
						
						
								 
								
								 
								 private
						 
						static
						 
						void
						 saveAttach(BodyPart part) 
						throws
						 Exception
    
				
				
						private
						 
						static
						 
						void
						 saveAttach(BodyPart part) 
						throws
						 Exception 
						
								 {
						
				
				
						
								{
 //
								得到未经处理的附件名字
        
								//
								得到未经处理的附件名字
						
						
								
								
										 String temp 
								=
						
						
								 part.getFileName();
								
						
						
								        String temp 
								=
						
						
								 part.getFileName();
 //
						
						
								除去发送邮件时,对中文附件名编码的头和尾,得到正确的附件名
        
								//
						
						
								除去发送邮件时,对中文附件名编码的头和尾,得到正确的附件名
 //
								(请参考发送邮件程序SendMail的附件名编码部分)
        
								//
								(请参考发送邮件程序SendMail的附件名编码部分)
						
						
								
								
										 String s 
								=
								 temp.substring(
								8
								, temp.indexOf(
								"
								?=
								"
						
						
								));
								
						
						
								        String s 
								=
								 temp.substring(
								8
								, temp.indexOf(
								"
								?=
								"
						
						
								));
 //
								文件名经过了base64编码,下面是解码
        
								//
								文件名经过了base64编码,下面是解码
						
						
								
								
										 String fileName 
								=
						
						
								 base64Decoder(s);
								
						
						
								        String fileName 
								=
						
						
								 base64Decoder(s);
 System.out.println(
								"
								有附件:
								"
								 
								+
						
						
								 fileName);
        System.out.println(
								"
								有附件:
								"
								 
								+
						
						
								 fileName);

 InputStream in 
								=
						
						
								 part.getInputStream();
        InputStream in 
								=
						
						
								 part.getInputStream();
 FileOutputStream writer 
								=
								 
								new
								 FileOutputStream(
								new
						
						
								 File(
        FileOutputStream writer 
								=
								 
								new
								 FileOutputStream(
								new
						
						
								 File(
 "
								保存附件的本地路径
								"
								+
								 
								"
								\\
								"
								+
						
						
								fileName));
                
								"
								保存附件的本地路径
								"
								+
								 
								"
								\\
								"
								+
						
						
								fileName));
 byte
								[] content 
								=
								 
								new
								 
								byte
								[
								255
						
						
								];
        
								byte
								[] content 
								=
								 
								new
								 
								byte
								[
								255
						
						
								];
 int
								 read 
								=
								 
								0
						
						
								;
        
								int
								 read 
								=
								 
								0
						
						
								;

 while
								 ((read 
								=
								 in.read(content)) 
								!=
								 
								-
								1
								)
        
								while
								 ((read 
								=
								 in.read(content)) 
								!=
								 
								-
								1
								) 
								
										 {
								
						
						
								
										{
 writer.write(content);
            writer.write(content);
 }
        }
								
						
						
								
								
										 writer.close();
        writer.close();
 in.close();
        in.close();
 }
    }
						
						
						
				
				
						
						
								 //
						base64解码
    
				
				
						//
						base64解码
				
				
						
						
								 
								 private
						 
						static
						 String base64Decoder(String s) 
						throws
						 Exception
						
				
				
						    
						private
						 
						static
						 String base64Decoder(String s) 
						throws
						 Exception 
						
								 {
						
				
				
						
								{
 sun.misc.BASE64Decoder decoder 
								=
								 
								new
						
						
								 sun.misc.BASE64Decoder();
        sun.misc.BASE64Decoder decoder 
								=
								 
								new
						
						
								 sun.misc.BASE64Decoder();
 byte
								[] b 
								=
						
						
								 decoder.decodeBuffer(s);
        
								byte
								[] b 
								=
						
						
								 decoder.decodeBuffer(s);
 return
								 (
								new
						
						
								 String(b));
        
								return
								 (
								new
						
						
								 String(b));
 }
    }
						
				
				
						
						
								 
								
								 
								 public
						 
						static
						 
						void
						 receive(String receiverMailBoxAddress, String username,String password)
    
				
				
						public
						 
						static
						 
						void
						 receive(String receiverMailBoxAddress, String username,String password) 
						
								 {
						
				
				
						
								{
 //
								本人用的是yahoo邮箱,故接受邮件使用yahoo的pop3邮件服务器
        
								//
								本人用的是yahoo邮箱,故接受邮件使用yahoo的pop3邮件服务器
						
						
								
								
										 String host 
								=
								 
								"
								pop.mail.yahoo.com.cn
								"
						
						
								;
								
						
						
								        String host 
								=
								 
								"
								pop.mail.yahoo.com.cn
								"
						
						
								;

 try
        
								try
								 
								
										 {
								
						
						
								
										{
 //
										连接到邮件服务器并获得邮件
            
										//
										连接到邮件服务器并获得邮件
								
								
										
										
												 Properties prop 
										=
										 
										new
								
								
										 Properties();
										
								
								
										            Properties prop 
										=
										 
										new
								
								
										 Properties();
 prop.put(
										"
										mail.pop3.host
										"
								
								
										, host);
            prop.put(
										"
										mail.pop3.host
										"
								
								
										, host);
 Session session 
										=
								
								
										 Session.getDefaultInstance(prop);
            Session session 
										=
								
								
										 Session.getDefaultInstance(prop);
 Store store 
										=
										 session.getStore(
										"
										pop3
										"
								
								
										);
            Store store 
										=
										 session.getStore(
										"
										pop3
										"
								
								
										);
 store.connect(host, username, password);
            store.connect(host, username, password);

 Folder inbox 
										=
										 store.getDefaultFolder().getFolder(
										"
										INBOX
										"
								
								
										);
            Folder inbox 
										=
										 store.getDefaultFolder().getFolder(
										"
										INBOX
										"
								
								
										);
 //
										设置inbox对象属性为可读写,这样可以控制在读完邮件后直接删除该附件
            
										//
										设置inbox对象属性为可读写,这样可以控制在读完邮件后直接删除该附件
								
								
										
										
												 inbox.open(Folder.READ_WRITE);
										
								
								
										            inbox.open(Folder.READ_WRITE);

 Message[] msg 
										=
								
								
										 inbox.getMessages();
            Message[] msg 
										=
								
								
										 inbox.getMessages();

 FetchProfile profile 
										=
										 
										new
								
								
										 FetchProfile();
            FetchProfile profile 
										=
										 
										new
								
								
										 FetchProfile();
 profile.add(FetchProfile.Item.ENVELOPE);
            profile.add(FetchProfile.Item.ENVELOPE);
 inbox.fetch(msg, profile);
            inbox.fetch(msg, profile);


 for
										 (
										int
										 i 
										=
										 
										0
										; i 
										<
										 msg.length; i
										++
										)
            
										for
										 (
										int
										 i 
										=
										 
										0
										; i 
										<
										 msg.length; i
										++
										) 
										
												 {
										
								
								
										
												{
 //
										
										
												标记此邮件的flag标志对象的DELETED位为true,可以在读完邮件后直接删除该附件,具体执行时间是在调用
                
												//
										
										
												标记此邮件的flag标志对象的DELETED位为true,可以在读完邮件后直接删除该附件,具体执行时间是在调用
 //
												inbox.close()方法的时候
                
												//
												inbox.close()方法的时候
										
										
												
												
														 msg[i].setFlag(Flags.Flag.DELETED, 
												true
										
										
												);
												
										
										
												                msg[i].setFlag(Flags.Flag.DELETED, 
												true
										
										
												);
 handleMultipart(msg[i]);
                handleMultipart(msg[i]);
 System.out.println(
												"
												****************************
												"
										
										
												);
                System.out.println(
												"
												****************************
												"
										
										
												);
 }
            }
										
								
								
										
										
												 
												 if
										 (inbox 
										!=
										 
										null
										)
            
								
								
										if
										 (inbox 
										!=
										 
										null
										) 
										
												 {
										
								
								
										
												{
 //
												参数为true表明阅读完此邮件后将其删除,更多的属性请参考mail.jar的API
                
												//
												参数为true表明阅读完此邮件后将其删除,更多的属性请参考mail.jar的API
										
										
												
												
														 inbox.close(
												true
										
										
												);
												
										
										
												                inbox.close(
												true
										
										
												);
 }
            }
										
								
								
										
										
												 
												 if
										 (store 
										!=
										 
										null
										)
            
								
								
										if
										 (store 
										!=
										 
										null
										) 
										
												 {
										
								
								
										
												{
 store.close();
                store.close();
 }
            }
										
								
								
										
										
												 
												 }
								
								
								
						
						
								 
								catch
								 (Exception e)
        }
								
								
								
						
						
								 
								catch
								 (Exception e) 
								
										 {
								
						
						
								
										{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
								
						
						
								
								
										 }
    }
						
						
						
				
				
						
						
								 }
}