2013-01-27 10 views
6

Podczas wysyłania załączników nie widzę wiadomości e-mail (message.setText (this.getEmailBody());) w wiadomości e-mail. Bez załączników wiadomość e-mail zostanie wyświetlona z treścią wiadomości. E-maile są wysyłane na konto Gmail. Jakąkolwiek wskazówkę, dlaczego tak się dzieje?Treść wiadomości nie jest wyświetlana podczas wysyłania załączników

 MimeMessage message = new MimeMessage(session_m);  
     message.setFrom(new InternetAddress(this.getEmailSender())); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.getEmailRecipient())); 
     message.setSubject(this.getEmailSubject()); 
     message.setText(this.getEmailBody()); //This won't be displayed if set attachments 

     Multipart multipart = new MimeMultipart(); 

     for(String file: getAttachmentNameList()){ 
      MimeBodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart.attachFile(this.attachmentsDir.concat(file.trim())); 
      multipart.addBodyPart(messageBodyPart); 

      message.setContent(multipart); 
     } 


     Transport.send(message); 
     System.out.println("Email has been sent"); 

Odpowiedz

9

Musisz użyć następujących:

  // Create the message part 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     // Fill the message 
     messageBodyPart.setText(body); 
     messageBodyPart.setContent(body, "text/html"); 

     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart); 
     //Add the bodypart for the attachment(s) 
     // Send the complete message parts 
     message.setContent(multipart); //message is of type - MimeMessage 
+0

to nie działa. – nidis

+0

Cała metoda. Mi to pasuje. Sprawdź, czy coś mi brakuje w opisie. Oto cała [metoda] (http://ideone.com/7ByeQs). – Srinivas

+2

masz rację. To działało również dla mnie. Myślę, że problem był w tych dwóch liniach: MimeBodyPart messageBodyPart = new MimeBodyPart(); \t \t messageBodyPart.attachFile (this.attachmentsDir.concat (file.trim())); Więc musiałem to zrobić po swojemu. Dzięki za pomoc;) – nidis

1

trzeba rozdzielić na 2 części, aby to zrobić:

 Multipart multipart = new MimeMultipart(); 

     // content part 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(content); 
     messageBodyPart.setContent(content, "text/html"); 
     multipart.addBodyPart(messageBodyPart); 

     BodyPart attachmentPart = new MimeBodyPart(); 
     DataSource source = new FileDataSource(file); 
     attachmentPart.setDataHandler(new DataHandler(source)); 
     attachmentPart.setFileName(file.getName()); 
     multipart.addBodyPart(attachmentPart); 
Powiązane problemy