JavaMail through POP3 receipt, attachment download, my method (code!) )

Source: Internet
Author: User
Tags count mail
Download connect to this servlet! from the attachment name

Downloadservlet.java

Package mailservlet;

Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;
Import javax.activation.*;
Import javax.mail.*;
Import javax.mail.internet.*;
/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2002</p>
* <p>Company:Socix</p>
* @author Simon
* @version 1.0
*/

public class Downloadservlet extends HttpServlet
{
public void Service (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception
{
HttpSession session = Request.getsession ();
String ContentType = Request.getparameter ("ContentType"); ContentType of the attachment
int count = Integer.parseint (Request.getparameter ("Count")); First few BodyPart
Object BODY = Session.getattribute ("Body"); Mimemultipart of Mail
if (body!= null)
{
System.out.println ("Download start!");
Try
{
Mimemultipart MP = (Mimemultipart) body;
int i = Mp.getcount ();
BodyPart part = Mp.getbodypart (count);
ContentType = Part.getcontenttype ();
String fileName = Part.getfilename ();
Response.setcontenttype (ContentType);
Response.setheader ("Content-disposition", "attachment; filename=" + filename);
System.out.println ("Download start!" + part.getsize ());
InputStream is = Part.getinputstream ();
Servletoutputstream out = Response.getoutputstream ();
Bufferedinputstream buf = new Bufferedinputstream (IS);
byte[] data = new byte[2048];
int readed = buf.read (data);
while (readed!=-1)
{
Out.write (data,0,readed);
readed = buf.read (data);
}
Out.flush ();
Out.close ();
Buf.close ();
System.out.println ("Download over!");
}
catch (Exception ex)
{
System.out.println ("error:" + ex);
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.