karplus
19-11-2005, 18:22
ciao, sto cercando di fare una classe java che tramite socket dialoga con un server smtp allo scopo di mandare una mail.
E' tutto ok, l'unico problema é uscire dal campo data dopo aver scritto il testo della mail; in una sessione telnet dopo aver scritto il testo della mail do invio, un punto e poi un altro invio ed esco dal campo del testo.
Qui ho cercato di far la stessa cosa con /n seguito da un punto e poi da /n ma non funge :muro:
Idee? :stordita:
riporto il sorgente qui sotto x completezza
import java.net.*;
import java.io.*;
import java.util.*;
public class SMTPTest {
private PrintWriter out;
private BufferedReader in;
public void send(String s) throws IOException {
if (s != null) {
out.println(s);
out.flush();
}
String line;
if ((line = in.readLine()) != null) //output the response
System.out.println(line);
}
public void sendMail() {
try {
String from = new String("prova@sdas.org");
String to = new String("miamail@tiscali.it");
Socket s = new Socket("smtp.virgilio.it", 25);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
send("HELO -");
send("MAIL FROM:<"+from+">");
send("RCPT TO:<"+to+">");
send("DATA");
send("Happy SMTP Programming!!");
send("Happy SMTP Programming!!");
send("\n");
send(".");
send("\n");
send("QUIT");
s.close();
out.close();
in.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SMTPTest smtp = new SMTPTest();
smtp.sendMail();
}
}
E' tutto ok, l'unico problema é uscire dal campo data dopo aver scritto il testo della mail; in una sessione telnet dopo aver scritto il testo della mail do invio, un punto e poi un altro invio ed esco dal campo del testo.
Qui ho cercato di far la stessa cosa con /n seguito da un punto e poi da /n ma non funge :muro:
Idee? :stordita:
riporto il sorgente qui sotto x completezza
import java.net.*;
import java.io.*;
import java.util.*;
public class SMTPTest {
private PrintWriter out;
private BufferedReader in;
public void send(String s) throws IOException {
if (s != null) {
out.println(s);
out.flush();
}
String line;
if ((line = in.readLine()) != null) //output the response
System.out.println(line);
}
public void sendMail() {
try {
String from = new String("prova@sdas.org");
String to = new String("miamail@tiscali.it");
Socket s = new Socket("smtp.virgilio.it", 25);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
send("HELO -");
send("MAIL FROM:<"+from+">");
send("RCPT TO:<"+to+">");
send("DATA");
send("Happy SMTP Programming!!");
send("Happy SMTP Programming!!");
send("\n");
send(".");
send("\n");
send("QUIT");
s.close();
out.close();
in.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SMTPTest smtp = new SMTPTest();
smtp.sendMail();
}
}