The class libraries provided by java do not support all your existing functions. You must write your own classes.

Source: Internet
Author: User

It's an old problem. I never dared to do it before, but now I have to do it. What is it?

You need to add attachments when sending emails in java. However, I have no chance to add real files, only data streams, because the file data streams I get from the database are too late to be stored on the hard disk, however, javamail only supports sending FileSourceData. What should I do? As a result, I decomcompiled the code, modified the interface I needed, and then implemented BlobSourceData for sending blob files. The content is simple, but it is a huge step. It gives me a lot of confidence.

  

/** BlobDataSource. java ** Created on 2008.7.9, am11: 48 ** To change this template, choose Tools | Template Manager * and open the template in the editor. */package mail; import java. io. inputStream; import java. io. outputStream; import java. io. file; import java. io. fileDescriptor; import java. io. fileNotFoundException; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. SQL. SQLException; import javax. activation. *; import oracle. SQL. BLOB; import com. sun. activation. registries. mimeTypeFile; public class BlobDataSource implements DataSource {private BLOB _ blob = null; private FileTypeMap typeMap = null; private String _ fileName = null; public BlobDataSource (String fileName, BLOB blob) {_ fileName = fileName; _ blob = blob;}/*** This method will return an InputStream representing the * the data and will throw an IOException if it can not do so. this method will return a new * instance of InputStream with each invocation. ** @ return an InputStream */public InputStream getInputStream () throws IOException {try {return _ blob. getBinaryStream ();} catch (SQLException expection) {return null ;}} /*** This method will return an OutputStream representing the * the data and will throw an IOException if it can * not do so. this method will return a new instance of * OutputStream with each invocation. ** @ return an OutputStream */public OutputStream getOutputStream () throws IOException {try {return _ blob. getBinaryOutputStream ();} catch (SQLException expection) {return null ;}/ *** This method returns the MIME type of the data in the form of a * string. this method uses the currently installed FileTypeMap. if * there is no FileTypeMap explictly set, the FileDataSource will * callgetDefaultFileTypeMapMethod on * FileTypeMap to acquire a default FileTypeMap.Note: By * default, the FileTypeMap used will be a MimetypesFileTypeMap.** @ Return the MIME Type * @ see javax. activation. fileTypeMap # getDefaultFileTypeMap */public String getContentType () {return "application/octet-stream";}/*** ReturnNameOf this object. the FileDataSource * will return the file name of the object. ** @ return the name of the object. * @ see javax. activation. dataSource */public String getName () {return _ fileName;}/*** Return the File object that corresponds to this FileDataSource. * @ return the File object for the file represented by this object. */public File getFile () {return null;}/*** Set the FileTypeMap to use with this FileDataSource ** @ param map The FileTypeMap for this object. */public void setFileTypeMap (FileTypeMap map) {typeMap = map ;}}

 

 

Its call code is as follows:

    public boolean addFileAffix(String filename,BLOB blob) {                Logger.println("add file affix:" + filename);                try {                        BodyPart bp = new MimeBodyPart();                        BlobDataSource streamds = new BlobDataSource(filename,blob);                        bp.setDataHandler(new DataHandler(streamds));                        bp.setFileName(streamds.getName());                        mp.addBodyPart(bp);                        return true;                    } catch (Exception e) {                        System.err.println("add file affix:" + filename + " error ecured" + e);                        return false;                    }            }
Related Article

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.