Javamail附加多个文件

时间:2016-06-15 22:57:48

标签: javamail

以下代码也使用javamail api通过gmail smtp服务器发送邮件和附件。

    public void doSendGmail(){
    from = txtFrom.getText();
    password= new String(txtPassword.getPassword());
    to = txtTo.getText();
    cc = txtCC.getText();
    bcc = txtBCC.getText();
    subject = txtSubject.getText();
    message_body = jtaMessage.getText();

    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,null);

    try {
        //message definition
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
        if(!cc.equals("")){
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
        }
        if(!bcc.equals("")){
            message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
        }
        message.setSubject(subject);
        if(!filename.equals("")){// if a file has been attached...
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(message_body);// actual message
            Multipart multipart = new MimeMultipart();// create multipart message

            // add the text message to the multipart
            multipart.addBodyPart(messageBodyPart);

            // attachment part
            messageBodyPart = new MimeBodyPart();
            String attachment = fileAbsolutePath;
            DataSource source = new FileDataSource(attachment);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);//add the attachment to the multipart message
            // combine text and attachment
            message.setContent(multipart);
            // send the complete message
            Transport.send(message, from, password);
        }
        else{// if no file has been attached
            message.setText(message_body);
            Transport.send(message, from, password);
        }

        JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.toString());
    }
}

此代码一次只能附加和发送一个文件。如何让它附加多个文件并将它们作为一封电子邮件发送?

使用JFileChooser附加文件,如下所示:

public void doAttachFile(){
    try {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        filename = file.getName();// get name of selected file
        lblFileName.setText(filename);// display name of selected file
        fileAbsolutePath= file.getAbsolutePath();
        System.out.println("File name: "+filename+"\n"+"Absolute path: "+fileAbsolutePath);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "No file was attached");
    }
}

3 个答案:

答案 0 :(得分:1)

使用" for"环绕附件部分。

答案 1 :(得分:0)

要发送多个文件,您可以实现此目的。

// Adds multiple attachments    
Multipart multipart = new MimeMultipart();

String[] attachFiles = new String[amountFiles];
attachFiles[0] = "path.pdf";
attachFiles[1] = "path.txt";

if(attachFiles != null && attachFiles.length > 0){
    for (String filePath : attachFiles) {
        MimeBodyPart attachPart = new MimeBodyPart();
        try {
            attachPart.attachFile(filePath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        multipart.addBodyPart(attachPart);
    }
}

message.setContent(multipart);

答案 2 :(得分:0)

您可以附加任意数量的文件,只需重复此行

即可
export.cypher
每个文件