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,0, chs.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,0, chs.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,0, bytes.length)) > 0)
{
if(len == bytes.length){
char chs [] = B64Code.encode(bytes);
out.write(chs,0, chs.length);
}
else{
byte b [] = new byte[len];
for(int i=0; i<len;i++)
b[i] = bytes[i];
char chs [] = B64Code.encode(b);
out.write(chs,0, chs.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...


Reply With Quote


Bookmarks