2016-02-03 38 views
0
public static String sendMail(
        String destino, 
        String texto, 
        String asunto, 
        byte[] formulario, 
        String nombre) { 

      Properties properties = new Properties(); 

      try{ 
      Session session = Session.getInstance(properties); 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      //Cargo el destino 
      if(destino!= null && destino.length > 0 && destino[0].length() > 0){ 
       for (int i = 0; i < destino.length; i++) { 
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(destino[i])); 
       } 
      } 
      //message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
      message.setSubject(asunto); 
      //I load the text and replace all the '&' for 'Enters' and the '#' for tabs 
      message.setText(texto.replaceAll("&","\n").replaceAll("#","\t")); 

      Transport.send(message); 

      return "Mensaje enviado con éxito"; 

     }catch(Exception mex){ 

      return mex.toString(); 
     } 

    } 

大家好。如何使用Java Mail在郵件中附加PDF?

我想弄清楚如何在前面顯示的代碼中附加由參數formulario發送的PDF。

公司用來做這件事以下伎倆,但他們需要改變其先前所示:

 MimeBodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(
      texto.replaceAll("&", "\n").replaceAll("#", "\t")); 
     //msg.setText(texto.replaceAll("&","\n").replaceAll("#","\t")); 
     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart); 

     messageBodyPart = new MimeBodyPart(); 

     messageBodyPart.setDataHandler(
      new DataHandler(
       (DataSource) new InputStreamDataSource(formulario, 
       "EDD", 
       "application/pdf"))); 

     messageBodyPart.setFileName(nombre + ".pdf"); 
     multipart.addBodyPart(messageBodyPart); 

     msg.setContent(multipart); 
     msg.saveChanges(); 

     Transport transport = session.getTransport("smtp"); 
     transport.connect(
      "smtp.gmail.com", 
      "[email protected]", 
      "companypassword"); 
     transport.sendMessage(msg, msg.getAllRecipients()); 
     transport.close(); 
     return "Mensaje enviado con éxito"; 
    } catch (Exception mex) { 
     return mex.toString(); 
+1

[使用javamail API發送帶有附件的電子郵件的可能的副本](http://stackoverflow.com/questions/17156544/sending-an-email-with-an-attachment-using-javamail-api) – glw

回答

1

採取

// Create a default MimeMessage object. 
Message message = new MimeMessage(session); 

// Set From: header field of the header. 
message.setFrom(new InternetAddress(from)); 

// Set To: header field of the header. 
message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(to)); 

// Set Subject: header field 
message.setSubject("Testing Subject"); 

// Create the message part 
BodyPart messageBodyPart = new MimeBodyPart(); 

// Now set the actual message 
messageBodyPart.setText("This is message body"); 

// Create a multipar message 
Multipart multipart = new MimeMultipart(); 

// Set text message part 
multipart.addBodyPart(messageBodyPart); 

// Part two is attachment 
messageBodyPart = new MimeBodyPart(); 
String filename = "/home/file.pdf"; 
DataSource source = new FileDataSource(filename); 
messageBodyPart.setDataHandler(new DataHandler(source)); 
messageBodyPart.setFileName(filename); 
multipart.addBodyPart(messageBodyPart); 

// Send the complete message parts 
message.setContent(multipart); 

// Send message 
Transport.send(message); 

System.out.println("Sent message successfully...."); 

原代碼是formulario在這兩種情況下的字節數組?如果是這樣,只需重寫第一塊代碼即可使用第二塊代碼中的技術構建消息。或者在新版本中用ByteArrayDataSource代替InputStreamDataSource。

2

既然你已經可以發送電子郵件,在調整你的代碼,並添加以下部分你的代碼here

+0

Hello並感謝您的回覆。我不明白如何添加它在'formulario'中的PDF的_content_ – Aoren

相關問題