邮件转发的思路:
1、 取得要转发的邮件信息(Message message)
2、 创建一个邮件信息体(Message forward)
3、 设置forward的主题(Subject)、源地址(From)、接收者(Recipient)
4、 创建Multipart的容器,在邮件转发里容器只要两个邮件体(BodyPart)就可以实现转发,一个是文字邮件部分,一个是被转发的邮件体(请看下面说明)
5、 讲Multipart容器放到转发邮件信息里,forward.setContent(multipart);
6、 发送邮件Transport.send(forward);
说明:思路3 源地址要与message的源地址一致
思路4 每个邮件是由多个部分组成,每个部分称为一个邮件体部分,是一个BodyPart类对象。BodyPart包含在Multipart容器里。
以下是一个经过测试可以运行的程序:
package com.mail;
2
3 import java.util.Properties;
4
5 import javax.mail. * ;
6 import javax.mail.internet. * ;
7
8 public class ForwardMail {
9
10 private static final String HOST_NAME = " pop.qq.com " ;
11 private static final String SEND_HOST_NAME = " smtp.qq.com " ;
12 private static final String PASSWORD = " ******** " ;
13 private static final String EMAIL_FROM = " ******@qq.com " ;
14 private static final String USER_NAME = " *******@qq.com " ;
15 private static final String PROTOCOL = " pop3 " ;
16 private static final String SEND_PROTOCOL = " smtp " ;
17
18 public static void forwardMail(Session session, Message message) throws Exception {
19 // 创建转发邮件信息
20 Message forward = new MimeMessage(session);
21 // 设置主题
22 forward.setSubject( " Fwd: " + message.getSubject());
23 forward.setFrom( new InternetAddress(EMAIL_FROM));
24 forward.addRecipient(Message.RecipientType.TO, new InternetAddress( " ********@sina.com " ));
25 // 文字邮件体部分
26 BodyPart messageBodyPart = new MimeBodyPart();
27 messageBodyPart.setText( " 邮件转发 " );
28 // 创建Multipart的容器
29 Multipart multipart = new MimeMultipart();
30 multipart.addBodyPart(messageBodyPart);
31 // 被转发的文字邮件体部分
32 messageBodyPart = new MimeBodyPart();
33 messageBodyPart.setDataHandler(message.getDataHandler());
34 // 添加到Multipart容器
35 multipart.addBodyPart(messageBodyPart);
36 forward.setContent(multipart);
37 // 发送
38 Transport.send(forward);
39 System.out.println( " msg forward .... " );
40 }
41
42 public static void listMail() throws Exception {
43 Properties props = new Properties();
44 props.put( " mail.smtp.host " , SEND_HOST_NAME);
45 props.put( " mail.pop.host " , HOST_NAME);
46 props.put( " mail.transport.protocol " , " smtp " );
47 props.put( " mail.smtp.auth " , " true " );
48
49
50 Session session = Session.getDefaultInstance(props, new SimpleAuthenticator(USER_NAME, PASSWORD));
51 Store store = session.getStore(PROTOCOL);
52 store.connect(HOST_NAME, USER_NAME, PASSWORD);
53 Folder folder = store.getFolder( " INBOX " );
54 folder.open(Folder.READ_ONLY);
55 Message[] messages = folder.getMessages();
56 InternetAddress address;
57 for ( int i = messages.length - 1 ; i >= 0 ; i -- ) {
58 address = (InternetAddress)messages[i].getFrom()[ 0 ];
59 /*
60 if(address != null) {
61 System.out.println(address.getPersonal());
62 }
63 */
64 if ( null != address && " 秋天的童话 " .equals(address.getPersonal())) {
65 System.out.println( " 第 " + i + " 个: " + messages[i].getSubject());
66 // 转发邮件
67 forwardMail(session, messages[i]);
68 }
69 }
70 folder.close( true );
71 store.close();
72 }
73
74 public static void main(String[] args) {
75 try {
76 listMail();
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 }
81 }
82
83