Send email

Hello,

i must wirte an application for sending an email (with attachment). I've used this class

import java.io.*;

import java.net.*;

publicclass Mail{

staticint SMTPport = 25;

static Socket socket;

static DataInputStream in;

static DataOutputStream out;

static PrintStream prout;

publicstaticboolean sendMail(

String mailServer,

String ricEmail,

String oggEmail,

String messaggio,

String fileName,

String userName,

String sendEmail){

try{

Socket s =new Socket(mailServer, SMTPport);

BufferedReader in =new BufferedReader(new InputStreamReader(s.getInputStream(),"8859_1") );

BufferedWriter out =new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"8859_1") );

String boundary ="Dat_Sep_Str_#COD#";// -- Data Separator String --

sendln(in, out,"HELO " + InetAddress.getLocalHost().getHostName());

sendln(in, out,"MAIL FROM: <"+ sendEmail +">");

sendln(in, out,"RCPT TO: <" + ricEmail +">" );

sendln(in, out,"DATA");

sendln(out,"MIME-Version: 1.0");

sendln(out,"Subject: " + oggEmail);

sendln(out,"From: " + userName +" <" + sendEmail +">");

sendln(out,"Content-Type: multipart/mixed; boundary=\"" + boundary +"\"");

sendln(out,"\r\n--" + boundary);

// invio il messaggio

sendln(out,"Content-Type: text/plain; charset=\"us-ascii\"\r\n");

sendln(out, messaggio);

sendln(out,"\r\n--" + boundary );

// invio l'allegato

String nomeFile = (new File(fileName)).getName();

sendln(out,"Content-Type:image/gif; name="+nomeFile);

sendln(out,"Content-Disposition: attachment;filename=\""+fileName+"\"");

sendln(out,"Content-transfer-encoding: base64\r\n");

MIMEBase64.encode(fileName, out);

sendln(out,"\r\n--" + boundary);

sendln(out,"\r\n\r\n--" + boundary +"--\r\n");

sendln(in, out,".");

sendln(in, out,"QUIT");

s.close();

returntrue;

}

catch (Exception e){

e.printStackTrace();

returnfalse;

}

}

// con questo metodo trametto i comandi smtp

publicstaticvoid sendln(BufferedReader in, BufferedWriter out, String s){

try{

out.write(s +"\r\n");

out.flush();

Thread.sleep(1500);// do tempo al server di rispondere

s = in.readLine();

}

catch (Exception e){

e.printStackTrace();

}

}

publicstaticvoid sendln(BufferedWriter out, String s){

try{

out.write(s +"\r\n");

out.flush();

}

catch (Exception e){

e.printStackTrace();

}

}

}

So, when i launch the application in Eclipse, it function correctly :)

Then i've created a .jar file with all application. When i try to do

java -jar mio.jar

it doesn't send email.

Why?

[5550 byte] By [PremierITAa] at [2007-11-27 6:56:18]
# 1
Because there's something wrong with your configuration.You haven't built any debugging options into your code, so you should probably start by fixing your code so you can see if it is connecting to the server. And if it is, what responses the server is sending.
DrClapa at 2007-7-12 18:33:14 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/products/javamail/javadocs/index.htmljavamail would be easier for you.Morzel
Morzela at 2007-7-12 18:33:14 > top of Java-index,Java Essentials,Java Programming...
# 3
In eclipse, this code function correctly!Why?
PremierITAa at 2007-7-12 18:33:14 > top of Java-index,Java Essentials,Java Programming...