下面是程序,但只能发送一个附件,不知道如何让下面的程序发送多个附件,谁能帮我看看啊,谢谢了
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;import java.io.UnsupportedEncodingException;
import java.util.*;
public class TestMail { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub try {
String host="smtp.163.com";
String user="邮箱名@163.com";
String passsword="密码";
String to="发送给谁@163.com";
String subject="多个附件标题";
String text="内容";
String attach="d:\\a.txt";//附件路径
// 获得属性,并生成Session对象
Properties props = new Properties();
Session sendsession;
Transport transport;
sendsession = Session.getInstance(props, null);
// 向属性中写入SMTP服务器的地址
props.put("mail.smtp.host",host);
// 设置SMTP服务器需要权限认证 props.put("mail.smtp.auth", "true");
// 设置输出调试信息
sendsession.setDebug(true);
// 根据Session生成Message对象
Message message = new MimeMessage(sendsession);
// 设置发信人地址
message.setFrom(new InternetAddress(user));
// 设置收信人地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// 设置e-mail标题
message.setSubject(subject);
// 设置e-mail发送时间 message.setSentDate(new Date());
// 设置e-mail内容
message.setText(text);
// 获得attachment参数


String attachment =attach;
// 如有附件
if (!attachment.equals("")) {
// 建立第一部分:文本正文 BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(text);
// 建立多个部分Multipart实例
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// 建立第二部分:附件

messageBodyPart = new MimeBodyPart();

attachment=attachment.replaceAll("\\\\", "/");

// 获得附件
DataSource source = new FileDataSource(attachment);
// 设置附件的数据处理器
messageBodyPart.setDataHandler(new DataHandler(source));
// 设置附件文件名

int   pos   =   attachment.lastIndexOf("/"); 
attachment   =   attachment.substring(pos + 1); 

//设置附件名字为中文时的编码,解决乱码问题
messageBodyPart.setFileName(MimeUtility.encodeText(attachment));
// 加入第二部分 
multipart.addBodyPart(messageBodyPart);
// 将多部分内容放到e-mail中 message.setContent(multipart);
} else {
// 如无附件,则按纯文本格式处理
message.setText(text);
}
}
// 保存对于e-mail的修改 message.saveChanges();
// 根据Session生成Transport对象
transport = sendsession.getTransport("smtp");
// 连接到SMTP服务器 transport.connect( host, user, passsword);
// 发送e-mail
transport.sendMessage(message, message.getAllRecipients());
// 关闭Transport连接
transport.close(); } catch (MessagingException m) {
System.out.println(m.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

解决方案 »

  1.   

          Multipart mp = new MimeMultipart(); 
          MimeBodyPart mbp = new MimeBodyPart(); 
          mbp.setContent(content.toString(), getContentType() + ";charset=" + getCharset()); 
          mp.addBodyPart(mbp); 
          if (!file.isEmpty()) {// 有附件 
            Enumeration efile = file.elements(); 
            while (efile.hasMoreElements()) { 
              mbp = new MimeBodyPart(); 
              filename = efile.nextElement().toString(); // 选择出每一个附件名 
              FileDataSource fds = new FileDataSource(filename); // 得到数据源 
              mbp.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart 
              mbp.setFileName(MimeUtility.encodeText(fds.getName(), getCharset(),"B")); // 得到文件名同样至入BodyPart 
              mp.addBodyPart(mbp); 
            } 
            file.removeAllElements(); 
          } 
          msg.setContent(mp); // Multipart加入到信件 原文: http://www.java2000.net/viewthread.jsp?tid=795