2013-08-06 85 views
1

有些人请告诉我如何附上pdf filemail。 我使用mail.jaractivation.jar发送mail。 我可以发送mail但我不知道如何发送pdf file与附件。 所以请建议我关于 谢谢。如何在java中附加生成的pdf文件邮件

我只是尝试

  String filename = "file.pdf"; 
      Multipart multipart1 = new MimeMultipart(); 
      BodyPart messageBodyPart = new MimeBodyPart(); 
      DataSource source = new FileDataSource(filename); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setFileName(filename); 
      multipart1.addBodyPart(messageBodyPart); 

但仍须没有得到数据,这空连接。

+0

要是你用Google搜索它,你甚至不需要问这个问题 – fge

回答

3

发送E-mail带附件使用JavaMail:

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

class SendAttachment 
{ 
    public static void main(String [] args) 
    {  
     String to="[email protected]";//change accordingly 
     final String user="[email protected]";//change accordingly 
     final String password="xxxxx";//change accordingly  

     //1) get the session object  
     Properties properties = System.getProperties(); 
     properties.setProperty("mail.smtp.host", "mail.javatpoint.com"); 
     properties.put("mail.smtp.auth", "true");  

     Session session = Session.getDefaultInstance(properties, 
       new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(user,password); } });  

     //2) compose message  
     try{  
      MimeMessage message = new MimeMessage(session);  
      message.setFrom(new InternetAddress(user));  
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
      message.setSubject("Message Aleart");   

      //3) create MimeBodyPart object and set your message text   
      BodyPart messageBodyPart1 = new MimeBodyPart();  
      messageBodyPart1.setText("This is message body");   

      //4) create new MimeBodyPart object and set DataHandler object to this object   
      MimeBodyPart messageBodyPart2 = new MimeBodyPart();  
      String filename = "SendAttachment.java";//change accordingly  
      DataSource source = new FileDataSource(filename);  
      messageBodyPart2.setDataHandler(new DataHandler(source));  
      messageBodyPart2.setFileName(filename);    

      //5) create Multipart object and add MimeBodyPart objects to this object   
      Multipart multipart = new MimeMultipart();  
      multipart.addBodyPart(messageBodyPart1);  
      multipart.addBodyPart(messageBodyPart2);  

      //6) set the multiplart object to the message object  
      message.setContent(multipart);   

      //7) send message  
      Transport.send(message);  
      System.out.println("message sent...."); 

     }catch (MessagingException ex) {ex.printStackTrace();} 
    } 
} 

来源:http://www.javatpoint.com/example-of-sending-attachment-with-email-using-java-mail-api

+0

我按照你的代码,文件附加但不包含数据,意味着附加0K。 – AloNE

+0

测试了代码正常工作。 –

+0

现在它给我的文件没有发现异常 – AloNE

0

试试这个:

 String SMTP_HOST_NAME = "mail.domain.com"; 
     String SMTP_PORT = "111"; 

     String SMTP_FROM_ADDRESS="[email protected]"; 
     String SMTP_TO_ADDRESS="[email protected]"; 
     String subject="Textmsg"; 
     String fileAttachment = "C:\\filename.pdf"; 

      Properties props = new Properties(); 

      props.put("mail.smtp.host", SMTP_HOST_NAME); 
      props.put("mail.smtp.auth", "true"); 
      props.put("mail.debug", "true"); 
      props.put("mail.smtp.port", SMTP_PORT); 
      Session session = Session.getInstance(props,new javax.mail.Authenticator() 
      {protected javax.mail.PasswordAuthentication 
       getPasswordAuthentication() 
      {return new javax.mail.PasswordAuthentication("[email protected]","password");}}); 
      try{ 

      Message msg = new MimeMessage(session); 

      msg.setFrom(new InternetAddress(SMTP_FROM_ADDRESS)); 
     // create the message part 
      MimeBodyPart messageBodyPart = 
      new MimeBodyPart(); 
     //fill message 
      messageBodyPart.setText("Test mail one"); 
      Multipart multipart = new MimeMultipart(); 
      multipart.addBodyPart(messageBodyPart); 
     // Part two is attachment 
      messageBodyPart = new MimeBodyPart(); 
      DataSource source = 
      new FileDataSource(fileAttachment); 
      messageBodyPart.setDataHandler( 
      new DataHandler(source)); 
      messageBodyPart.setFileName(fileAttachment); 
      multipart.addBodyPart(messageBodyPart); 
     // Put parts in message 
      msg.setContent(multipart); 


      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SMTP_TO_ADDRESS)); 

      msg.setSubject(subject); 
     // msg.setContent(content, "text/plain"); 

      Transport.send(msg); 
      System.out.println("success...................................."); 
      } 
      catch(Exception e){ 
       e.printStackTrace();   
      } 

来源:http://www.coderanch.com/t/586537/java/java/sample-code-java-mail-api

0

您应该创建MimeMultipartMimeBodyPartFileDataSourceDataHandler如下:

Multipart multipart = new MimeMultipart(); 
messageBodyPart = new MimeBodyPart(); 
String filePath = "<file system path>"; 
String fileName = "display name" 

DataSource source = new FileDataSource(file); 
messageBodyPart.setDataHandler(new DataHandler(source)); 
messageBodyPart.setFileName(fileName); 
multipart.addBodyPart(messageBodyPart); 

现在,MimeMessage使用setContent方法。

相关问题