2016-09-02 5 views
1

は、ランダムな形式の添付ファイルを取得しています。そのオブジェクトZip MIMEタイプを使用してメールを送信しようとしているときに、春の添付ファイル付きメールを送信する方法

package com.mail.send; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MailImpl { 
public static void main(String args[]) throws IOException{ 

    ApplicationContext context=new ClassPathXmlApplicationContext("Spring.xml"); 
    JavaMail javaMail=(JavaMail) context.getBean("JavaMail"); 
    System.out.println("after bean creation"); 
    String to="[email protected]"; 
    String cc="[email protected]"; 
    String subject="Pdf notification"; 
    String content="PDF has been generated"; 
    boolean flag = true ; 
    File file =new File("E:\\Sample.pdf"); 

    //byte [] sample=null; 

    /*start of modify*/ 
    FileInputStream fin = null; 
    byte fileContent[] = new byte[(int)file.length()]; 
    try { 
     // create FileInputStream object 
     fin = new FileInputStream(file); 



     // Reads up to certain bytes of data from this input stream into an array of bytes. 
     fin.read(fileContent); 
     //create string from byte array 
     String s = new String(fileContent); 
     System.out.println("File content: " + s); 
    } 
    catch (FileNotFoundException e) { 
     System.out.println("File not found" + e); 
    } 
    catch (IOException ioe) { 
     System.out.println("Exception while reading file " + ioe); 
    } 
    finally { 
     // close the streams using close method 
     try { 
      if (fin != null) { 
       fin.close(); 
      } 
     } 
     catch (IOException ioe) { 
      System.out.println("Error while closing stream: " + ioe); 
     } 
    } 
    byte[] fileContent1 = null; 

    /*end of modify */ 
    if(flag==true){ 
     fileContent1=zipBytes("Sample.pdf", fileContent);//document.getmimetype 
    }else{ 
     fileContent1 = fileContent; 
    } 
    javaMail.sendEmail(to, cc, subject, content, fileContent1); 


} 

public static byte[] zipBytes(String filename, byte[] input) throws IOException { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ZipOutputStream zos = new ZipOutputStream(baos); 
    ZipEntry entry = new ZipEntry(filename); 
    entry.setSize(input.length); 
    zos.putNextEntry(entry); 
    zos.write(input); 
    zos.closeEntry(); 
    zos.close(); 



    return baos.toByteArray(); 
} 


} 

答えて

0

パッケージcom.mail.send bytearraysourceからMIMEタイプを取得する方法を知っておく必要があり、ハードコーディングにより、MIMEタイプを変更しようとしました。

import java.io.File; 
import javax.mail.internet.MimeMessage; 
import javax.mail.util.ByteArrayDataSource; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.mail.javamail.JavaMailSender; 
import org.springframework.mail.javamail.MimeMessageHelper; 

// mailimpl パブリッククラスのJavaMail {

@Autowired 
JavaMailSender mailSender; 

public JavaMailSender getMailSender() { 
    return mailSender; 
} 

public void setMailSender(JavaMailSender mailSender) { 
    this.mailSender = mailSender; 
} 

String from = "[email protected]"; 

public void sendEmail(String to, String cc, String subject, String content, 
     byte [] attachement) { 
    try { 
     MimeMessage mimeMessage = mailSender.createMimeMessage(); 
     MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true); 
     mimeMessageHelper.setFrom(from); 
     mimeMessageHelper.setTo(to); 
     mimeMessageHelper.setCc(cc); 
     mimeMessageHelper.setSubject(subject); 
     mimeMessageHelper.setText(content, true); 
    // mimeMessageHelper.addAttachment(attachement.getName(), attachement); 

     //ByteArrayDataSource byteArrayDataSource=new ByteArrayDataSource(attachement, "application/zip"); 
     ByteArrayDataSource byteArrayDataSource = null; 
     if(attachement.length > 10){ 
      byteArrayDataSource=new ByteArrayDataSource(attachement, "application/zip"); 
      mimeMessageHelper.addAttachment(byteArrayDataSource.getName(), byteArrayDataSource); 

      }else{ 
       byteArrayDataSource=new ByteArrayDataSource(attachement, "application/pdf"); 
       mimeMessageHelper.addAttachment(byteArrayDataSource.getName()+".pdf", byteArrayDataSource); 

      } 
     //mimeMessageHelper.addAttachment(byteArrayDataSource.getName(), byteArrayDataSource); 

     System.out.println("Start of mail"); 
     mailSender.send(mimeMessage); 
     System.out.println("End of mail"); 

    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 

} 


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:component-scan base-package="com.mail.send" /> 

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
    <property name="host" value="localhost" /> 
    <property name="port" value="25" /> 
    <property name="javaMailProperties"> 
     <props> 
      <prop key="mail.transport.protocol">smtp</prop> 
      <prop key="mail.smtp.auth">false</prop> 
      <prop key="mail.smtp.starttls.enable">true</prop> 
     </props> 
    </property> 
</bean> 

<bean id="JavaMail" class="com.mail.send.JavaMail"> 
    <property name="mailSender" ref="mailSender" /> 
</bean> 

<!-- <bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    max upload size in bytes 
    <property name="maxUploadSize" value="20971520" /> 20MB 

    max size of file in memory (in bytes) 
    <property name="maxInMemorySize" value="1048576" /> 1MB 

</bean> --> 
</beans> 
関連する問題