简体   繁体   中英

Java via SMTP send attachment with HTML file

I send attachment mail...

This source is parsing from html to html, and works well:

SmtpClient sm = new SmtpClient(selSMTPServer(from));
sm.from(from_mail);

sm.to(to);

PrintStream msg = sm.startMessage();
//Base64Encoder b64e = new Base64Encoder(msg);
BASE64EncoderStream b64e = new BASE64EncoderStream(msg);
//org.jboss.security.Base64Encoder  b64e = new Base64Encoder();


msg.print("From: ");
msg.println(from_name+"<"+from_mail+">");

msg.print("To: ");
msg.println(to_name+"<"+to+">");

msg.print("Subject: ");
msg.println(subject);
msg.println("MIME-Version: 1.0");

msg.println("Content-Type: multipart/mixed;");  

msg.println("\tboundary=\"----=_next_part\"");
msg.println("\r");
msg.println("This is a multipart message in MIME format.");
msg.println("\r");

msg.println("------=_next_part");
msg.println("Content-Type:text/html; charset=\"euc-kr\"");
msg.println("Content-Transfer-Encoding: 8bit");
msg.println("\r");
msg.println(body);
msg.println("\r");


int  fileNameLength = fileName.length;

for (int k=1;k < fileNameLength; k++){
    if (!fileName[k].equals(""))
    {
        /* attachment file */

        if (k >1)
        {
            msg.println("\r\n");
        }

        msg.println("------=_next_part");
        msg.println("Content-Type: application/octet-stream;");
        msg.println("\tname=\""+fileName[k]+"\"");
        msg.println("Content-Transfer-Encoding: base64");
        msg.println("Content-Disposition: attachment;");
        msg.println("\tfilename=\""+fileName[k]+"\"");
        msg.println("\r\n");

        File f = new File(filePath+fileName[k]);
        FileInputStream fis = new FileInputStream(f);
        int n = 0;

        do{
            byte[] fcontent = {0, 0, 0};
            n = fis.read(fcontent, 0, 3);
            if (n != -1){
                b64e.write(fcontent);
            }
        }while(n != -1);

        fis.close();
        fis = null;
        f = null;

However Created file is decreased...

ex)

created file

 <div class="col-md-6 text-right" style="padding-top:30px;">                <p class="exhibition-info">Tel. <span class="exhibition-tel">111-111-111</span> / Fax. <span class="exhibition-fax"></span></p>                <p class="dealer-info"><span class="dealer-hp">111-111-111</span> / <span class="dealer-mail">kwang@dongsung.com</span></p>            </div>            <div cl

original source

      <div class="col-md-6 text-right" style="padding-top:30px;">                <p class="exhibition-info">Tel. <span class="exhibition-tel">111-111-111</span> / Fax. <span class="exhibition-fax"></span></p>                <p class="dealer-info"><span class="dealer-hp">111-111-111</span> / <span class="dealer-mail">kwang@dongsung.com</span></p>            </div>            <div class="col-md-12" style="padding-top:30px;">

Why do these problems occur?

I've solved the problem.
work well.

do{
    byte[] data = byte[1024];
    n = fis.read(data, 0, data.length);
    if (n != -1)
    {
      b64e.write(data);
    }
  }while(n != -1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM