如何在java中发送带有多个附件的邮件

时间:2015-02-24 03:04:31

标签: java email javamail email-attachments

我这里可以发送带有多个附件的邮件:

Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();

// creates body part for the message
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");

//set message body
BodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setText(body);
multipart.addBodyPart(msgBodyPart);
msgBodyPart = new MimeBodyPart();

//attach file
DataSource source = new FileDataSource(attachFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachFile);
multipart.addBodyPart(messageBodyPart);

//attach file 2
source = new FileDataSource(attachFile2);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(attachFile2);
multipart.addBodyPart(messageBodyPart2);

try {
    message.setFrom(new InternetAddress(from));
    InternetAddress[] toAddress = new InternetAddress[to.length];

      // To get the array of addresses
    for( int i = 0; i < to.length; i++ ) {
        toAddress[i] = new InternetAddress(to[i]);
    }

    for( int i = 0; i < toAddress.length; i++) {
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }
} catch (MessagingException ex) {
    ex.printStackTrace();
}

message.setSubject(subject);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

但问题是如何添加多个附件?我不知道我是否可以声明许多变量或将其放在数组上。该代码只能包含2个附件,如果每次发送电子邮件中的5个或任何附件,

3 个答案:

答案 0 :(得分:4)

创建一个名为attachFile的简单方法,该方法将FileMultipartMimeBodyPart作为参数......

public void attachFile(File file, Multipart multipart, MimeBodyPart messageBodyPart) {
    DataSource source = new FileDataSource(file);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(file.getName());
    multipart.addBodyPart(messageBodyPart);    
}

根据需要随时调用

File attachFiles[] = ...

if (attachFiles > 0) {
    //attach file
    attachFile(attachFiles[0], multipart, messageBodyPart);
    if (attachFiles > 1) {
        for (int index = 1; index < attachFiles.length; index++) {
            attachFile(attachFiles[0], multipart, new MimeBodyPart());
        }
    }
}

作为例子

答案 1 :(得分:2)

使用Java Mail 1.4及更高版本可以使附件更简单,

messageBodyPart.attachFile(String filePath)

multipart.addBodyPart(messageBodyPart);

function onclickfunction(e) {
    ... code ...
    if (<event is handled>) {
        e.stopPropagation();
    }
}

答案 2 :(得分:0)

看看这里给出的答案

https://stackoverflow.com/a/3177640/772590

第1步   创建数据源

DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

步骤2创建添加附件的方法

private static void addAttachment(Multipart multipart, String filename)
{
    DataSource source = new FileDataSource(filename);
    BodyPart messageBodyPart = new MimeBodyPart();        
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
}

步骤3调用上面的方法添加附件

addAttachment(multipart, "file1.txt");
addAttachment(multipart, "file2.txt");