2016-06-15 92 views
3

以下代码通过使用javamail api的gmail smtp服务器发送邮件和附件。Javamail附加多个文件

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"); 
    } 
} 

回答

1

使用围绕所述附接部的“for”循环。

+0

我使用JFileChooser,以附加的文件(你可以从编辑看到您可以将尽可能多的文件源代码)。如何将附加文件存储在数组中,以便可以使用for循环将附加文件添加到消息中。如果代码保持原样,则只会发送最后附加的文件。 – David

+0

我不确定您是否对非常基本的Java编程感到困惑,或者不知道如何使用JFileChooser来选择多个文件。我相信JFileChooser将只允许您一次选择一个文件,因此您需要围绕使用JFileChooser进行循环,并以某种方式让用户说出“我完成了选择文件”。每次迭代都会将所选文件添加到数组或列表或其他内容。 –

+0

我对使用JFileChooser来选择多个文件并不是很困惑,而是如何将多个附件添加到多部分。我正在考虑创建一个数组来添加附加的任何文件,然后在完成附加文件后将数组添加到多部分。问题是,什么数据类型将是数组,以便它能够保存文件 – David

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); 
0

,只要你想只要重复这条线

multipart.addBodyPart(messageBodyPart); 

每个文件