Announcement Announcement Module
Collapse
No announcement yet.
How to set attachment 's encoding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set attachment 's encoding

    I use springframework 's mail API to send mail. And I am Chinese; I find that the email 's attachment couldn't be open normally. it display a wrong encoding. but the subject and the text display normally. now I want to know how to set the encoding . the code I write in below:

    MultipartHttpServletRequest request=(MultipartHttpServletRequest) rst;

    request.setCharacterEncoding("gb2312");
    String subject=request.getParameter("subject").trim();
    String sendTo=request.getParameter("sendTo").trim();
    String text=request.getParameter("text").trim();

    MultipartFile f=(MultipartFile)(request.getFile("attachement"));

    MimeMessage message = this.getJavaMailSender().createMimeMessage();
    // use the true flag to indicate you need a multipart message

    MimeMessageHelper helper = new MimeMessageHelper(message,true,"gb2312");
    helper.setFrom("[email protected]");
    helper.setTo("sendTo");

    // use the true flag to indicate the text included is HTML
    helper.setText(text);
    helper.setSubject(subject);
    if(f.getSize()!=0){
    MyInputStream s = new MyInputStream();
    ByteArrayInputStream in = (ByteArrayInputStream) f.getInputStream();
    s.setInputStream(in);
    helper.addAttachment(f.getOriginalFilename(), s);
    helper.getEncoding();

    }
    try{
    this.getJavaMailSender().send(message);
    }catch(Exception e){
    e.printStackTrace();
    }
    return new ModelAndView(new RedirectView("welcome.sf"));

    Code:
    	class MyInputStream implements InputStreamSource{
    
    
    		private ByteArrayInputStream in;
    		public InputStream getInputStream() throws IOException {
    			
    			return this.in;
    		}
    		public void setInputStream(ByteArrayInputStream in){
    			this.in=in;
    		}
    could anyone give me a example or some suggestion?

  • #2
    First of all, you don't need to create a class like MyInputStream: If you want to pass an existing InputStream in, use Spring's InputStreamResource - which implements the InputStreamSource interface, wrapping a passed-in InputStream.

    Regarding the encoding of the attachment: I assume you're referring to a text/HTML attachment that's supposed to be opened automatically? Note that an attachment will typically be treated as binary and thus only have a content type but not a specific encoding. The only way to specify the encoding (as far as I know) is to attach it to the content type String, as known from JSP.

    Consider using the addAttachment(attachmentFilename, dataSource) method which expects a Java Activation Framework DataSource as argument. Such a DataSource can easily be implemented as anonymous inner class: It needs to expose the InputStream for the attachment, the content type (potentially also indicating a specific encoding), and the name of the attachment (usually the filename).

    On this occasion, I've just added an overloaded convenience method to MimeMessageHelper: addAttachment(attachmentFilename, inputStreamSource, contentType). This method takes an explicit content type argument, not extracting the content type from the attachment filename. This is just a convenience, though: addAttachment(attachmentFilename, dataSource) should work as well, with more lines of code though.

    Juergen

    Comment


    • #3
      Originally posted by Juergen Hoeller
      First of all, you don't need to create a class like MyInputStream: If you want to pass an existing InputStream in, use Spring's InputStreamResource - which implements the InputStreamSource interface, wrapping a passed-in InputStream.

      Juergen
      I have tried to do use the InputStream and I get a exception I can't resolve it ,so I create a new inner class which implements the InputStreamSource interface wrapping a passed-in InputSteam.

      the Exception is:
      InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
      the below is my code:


      Code:
      		MultipartHttpServletRequest request = (MultipartHttpServletRequest) rst;
      
      		MultipartFile file1 = request.getFile("attachment1");
      		MultipartFile file2 = request.getFile("attachment2");
      		String text=request.getParameter("text");
      		String subject=request.getParameter("subject");
      		List enterprises=null;
      		//给组的成员发送信息 
      		String groupId=request.getParameter("groupId");	
      		if(groupId!=null&&!groupId.equals("")){
      		
      		Group group=(Group)this.getGroupDao().load(groupId);
      		enterprises=this.getEnterpriseDao().findByGroup(groupId);
      		for&#40;int i=0;i<enterprises.size&#40;&#41;;i++&#41;&#123;
      			CustomerEntity customerEntity=&#40;CustomerEntity&#41;enterprises.get&#40;i&#41;;	
      			//给每一个客户发送邮件
      			MimeMessage message = this.getJavaMailSender&#40;&#41;.createMimeMessage&#40;&#41;;
      			
      			//		 use the true flag to indicate you need a multipart message
      			MimeMessageHelper helper = new MimeMessageHelper&#40;message,true&#41;;
      			helper.setFrom&#40;"[email protected]"&#41;;
      			helper.setTo&#40;customerEntity.getEmail&#40;&#41;&#41;;
      			helper.getEncoding&#40;&#41;;
      			//		 use the true flag to indicate the text included is HTML
      			helper.setText&#40;text&#41;;
      			helper.setSubject&#40;subject&#41;;
      			if&#40;file1.getSize&#40;&#41;!=0&#41;&#123;
      
      				InputStreamResource s=new InputStreamResource &#40;file1.getInputStream&#40;&#41;,"测试1"&#41;;
      				helper.addAttachment&#40;file1.getOriginalFilename&#40;&#41;,s&#41;;
      			&#125;
      			if&#40;file2.getSize&#40;&#41;!=0&#41;&#123;
      				InputStreamResource t=new InputStreamResource &#40;file2.getInputStream&#40;&#41;,"测试2"&#41;;
      				helper.addAttachment&#40;file2.getOriginalFilename&#40;&#41;,t&#41;;
      			
      			&#125;
                  try&#123;
                  	this.getJavaMailSender&#40;&#41;.send&#40;message&#41;;
                  &#125;catch&#40;Exception e&#41;&#123;
      //            	throw new SendMailException&#40;&#41;;
                  	e.printStackTrace&#40;&#41;;
                  &#125;
      		  &#125;
      		return new ModelAndView&#40;new RedirectView&#40;"detailGroup.sf?groupId="+groupId&#41;,"mailInfo","true"&#41;;
      could anyone tell me why?The Exception tell us that the InputStream couldnt' be read for many times.so are there any other methods to deal with this problem? and could anybody share his code as a example.The method I have used could send Email with attachment but with wrong encoding. and I have two attachment in my form. so what should do?

      thanks in advance.

      Comment


      • #4
        me too! the same question! pls help !

        could anyone can tell me why? and give the demo!
        thanks a lots;

        Comment

        Working...
        X