用Java转发电子邮件

我们可以在发送电子邮件时将收到的邮件转发给其他人。有许多javamail类用于将消息转发到目标资源。

为了更好地理解此示例, 请首先学习使用JavaMail API发送电子邮件的步骤。
为了使用JavaMail API接收或发送电子邮件, 你需要加载两个jar文件:mail.jar activation.jar下载这些jar文件(或)到Oracle网站下载最新版本。
用Java转发电子邮件的示例
import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class ForwardMail{ public static void main(String[] args)throws Exception { final String user="sonoojaiswal@srcmini.com"; //change accordingly final String password="xxxxx"; //change accordingly//get the session object Properties props = new Properties(); props.put("mail.smtp.host", "mail.srcmini.com"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); // Get a Store object and connect to the current host Store store = session.getStore("pop3"); store.connect("mail.srcmini.com", user, password); //Create a Folder object and open the folder Folder folder = store.getFolder("inbox"); folder.open(Folder.READ_ONLY); Message message = folder.getMessage(1); // Get all the information from the message String from = InternetAddress.toString(message.getFrom()); if (from != null) { System.out.println("From: " + from); } String replyTo = InternetAddress.toString( message.getReplyTo()); if (replyTo != null) { System.out.println("Reply-to: " + replyTo); } String to = InternetAddress.toString( message.getRecipients(Message.RecipientType.TO)); if (to != null) { System.out.println("To: " + to); }String subject = message.getSubject(); if (subject != null) { System.out.println("Subject: " + subject); } Date sent = message.getSentDate(); if (sent != null) { System.out.println("Sent: " + sent); } System.out.println(message.getContent()); // compose the message to forward Message message2 = new MimeMessage(session); message2.setSubject("Fwd: " + message.getSubject()); message2.setFrom(new InternetAddress(from)); message2.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Create your new message part BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Oiginal message:\n\n"); // Create a multi-part to combine the parts Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Create and fill part for the forwarded content messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(message.getDataHandler()); // Add part to multi part multipart.addBodyPart(messageBodyPart); // Associate multi-part with message message2.setContent(multipart); // Send message Transport.send(message2); System.out.println("message forwarded ...."); } }

【用Java转发电子邮件】如上例所示, 我们能够将电子邮件转发到目标资源。现在通过以下方式运行该程序:
加载jar文件 c:\> set classpath = mail.jar; activation.jar; 。;
编译源文件 c:\> javac ForwardMail.java
由… … 运营 c:\> Java ForwardMail

    推荐阅读