implements SMTP send an attachment


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: implements SMTP send an attachment

  1. #1
    Join Date
    May 2005
    Posts
    2

    Post implements SMTP send an attachment

    hello everyone..

    i'm write a simple program that can send an email with an attachment.
    so far, everything works fine, however, i don;t really understand why with
    the following changes will make it doesn't work

    [Before change ]
    all the code is in java language. ( i use php tag to make the improve the readablity of them.)
    PHP Code:
    String CRLF ="\r\n";
     
             
    SocketFactory socketFactory SSLSocketFactory.getDefault();
             
    socket socketFactory.createSocket("smtp.gmail.com"465);
             
    in = new  BufferedReader(new   InputStreamReader(socket.getInputStream()));
             
    out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
         
               
    //  HELO  Xmailerr
               //   AUTH login
               //  Data 
               //  from
               //  ................  
    out.write("Mime-Version: 1.0");                     
    out.write(CRLF);  out.flush();
    out.write("Content-Type: multipart/mixed; "+CRLF +" boundary=\"" boundary "\""); 
    out.write(CRLF); out.flush(); 
    out.write(CRLF); out.flush();
       
            
    // text part
     
           // attachment part
                
    File file =  new File("Track3.mp3");
                
    out.write("--" boundary); out.write(CRLF); out.flush();
                
    out.write("Content-Type: application/octet-stream; name="+  file.getName());     out.write(CRLF); out.flush();
                
    out.write("Content-Disposition: attachment;  filename=\""+file.getName()+"\""); out.write(CRLF); out.flush();
                
    out.write("Content-Transfer-Encoding: base64");                                 out.write(CRLF); out.flush();           
             
                
    out.write(CRLF);out.flush();
            
    // read the file  (i wanna to change above codes)
                
    FileInputStream fis = new FileInputStream(file);
                
    int av  fis.available();
                
    byte bytes [] = new byte [av];
                
    fis.read(bytes);
                
    char chs [] = B64Code.encode(bytes);
                
    out.write(chs,0chs.length); 
                
                
    out.write(CRLF); out.flush();
                
    out.write("--" boundary "--"); out.write(CRLF);out.flush();
                
    out.write(CRLF); out.write(".");  out.write(CRLF); out.flush();
                
    System.out.println((in.readLine());
                
    out.write("QUIT");
                
    out.flush(); 

    after change
    because i wanna send a very large of file ( 5mb) and i don;t want it load to memory at once..

    PHP Code:
     byte bytes [] = new byte [av];
               
    fis.read(bytes);
               
    char chs [] = B64Code.encode(bytes);
               
    out.write(chs,0chs.length); 
    since the above code that used to much memory if the file is large., then i decided to changed it to following code.


    PHP Code:
    byte bytes [] = new byte [1024];
                 
    int len = -1;
                while((
    len fis.read(bytes,0bytes.length)) > 0)
                 {
                   if(
    len == bytes.length){
                      
    char chs [] = B64Code.encode(bytes);
                      
    out.write(chs,0chs.length);
                        }
                   else{
                      
    byte b [] = new byte[len];
                      for(
    int i=0i<len;i++)
                           
    b[i] = bytes[i];
                      
    char chs [] = B64Code.encode(b);
                      
    out.write(chs,0chs.length); 
                     } 
                }
                
    out.write(CRLF); out.flush();
                
    out.write("--" boundary "--"); out.write(CRLF);out.flush();
                
    out.write(CRLF); out.write(".");  out.write(CRLF); out.flush();
                
    System.out.println((in.readLine());
                
    out.write("QUIT");
                
    out.flush(); 


    after changed it , i tried to send another file, it no longer work for me,
    i opend my gmail account, and checked the mail , it showed that only
    1kb for the attachment,

    however, I clicked "More Option, --- Show original" i can see the whole message that is encode in 64. not only 1kb..


    thanks for you help in advance...

  2. #2
    Join Date
    May 2005
    Posts
    2
    I know what cause the problem..

    becouse i used a while loop, and it reads the byte [] is very fast...
    and the newly readed byte [] will replace the previouse byte [] has not sent out..

    i need some blocking method here....

    can anyone tell me how to do that, make sure the previouse byte [] has write out, then write next byte []

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center