2

I have the following code to send an email with multiple parts

 public void sendEmail(String emailAddress, List<String> attachment) throws Exception{
    Properties props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");

    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(true);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    // message subject
    message.setSubject("Automated email from Kieran Herley about Assignments");

    message.addRecipient(Message.RecipientType.TO,
         new InternetAddress(emailAddress));

    Multipart multipart = new MimeMultipart();
    MimeBodyPart messageBodyPart = new MimeBodyPart();

    // message body
    messageBodyPart.setText("This is just a message to say your assignment has been graded.\nAttached is a file with some pointers about your assignment");
    multipart.addBodyPart(messageBodyPart);

    messageBodyPart = new MimeBodyPart();

    for (String singleFile : attachment) {
        DataSource source = new FileDataSource(singleFile);
        messageBodyPart.setDataHandler(new DataHandler(source));
        String nameOfFile = singleFile.substring(singleFile.lastIndexOf('\\') + 1);
        messageBodyPart.setFileName(nameOfFile);
        multipart.addBodyPart(messageBodyPart);
    }
    message.setContent(multipart);


    transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

    transport.sendMessage(message,
        message.getRecipients(Message.RecipientType.TO));
    transport.close();
}

Everything is working fine but the problem i am having is in the for-loop

for (String singleFile : attachment) {
        DataSource source = new FileDataSource(singleFile);
        messageBodyPart.setDataHandler(new DataHandler(source));
        String nameOfFile = singleFile.substring(singleFile.lastIndexOf('\\') + 1);
        messageBodyPart.setFileName(nameOfFile);
        multipart.addBodyPart(messageBodyPart);
    }
    message.setContent(multipart);

The problem that i am having is, if the application loops through the for-loop 3 times, it will name all the files attached to the email the name of the file that was passed through the loop the third time. This is also the same with the email attachment itself, even do it is sending 3 attachments, they are all named the same.

This is because all three of the attachments are using the same variable to both name them and retrieve so for all three of them it is naming them and setting them as the last one passed through the loop.

Is there a way to set the name and attachment of each email by using the same name variable and attachment variable for all, or how would I do this?

In this case use

String singleFile --- for the attachment
String nameOfFile --- for the name of each file
4

2 回答 2

3

我不确定我是否了解您的需求,但是拥有这条线可能很有意义:

messageBodyPart = new MimeBodyPart();

在 for 循环内,而不是之前。目前,您不断将相同的身体部位添加到multipart.

于 2012-09-07T14:42:00.257 回答
3

对于 for-each 循环中的字符串,这看起来没什么问题。我担心您总是使用相同的实例来messageBodyPart分配数据处理程序和文件名。尝试messageBodyPart = new MimeBodyPart()在循环内移动。

于 2012-09-07T14:42:25.277 回答