Ciao a tutti,
su internet ho trovato il seguente script per inviare mail
codice:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
public class MailServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -9039413964097796343L;
String MAIL_SERVER_URL = "127.0.0.1";
int MAIL_PORT = 25;
Socket socket;
DataInputStream in;
PrintStream out;
String str;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out;
SendMail("From", "To", "subj", "text");
response.setContentType("text/html");
out = response.getWriter();
out.println("n");
out.println("n");
out.println("SendMailn");
out.println("n");
out.println("n");
out.close();
}
public void SendMail(String to, String from, String subject, String message) throws ProtocolException, IOException {
Socket socket = new Socket(MAIL_SERVER_URL, MAIL_PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
if (!checkStatus("220")) {
throw new ProtocolException(str);
}
out.println("HELO " + MAIL_SERVER_URL);
out.flush();
if (!checkStatus("250")) {
throw new ProtocolException(str);
}
out.println("MAIL FROM: " + from);
out.flush();
if (!checkStatus("250")) {
throw new ProtocolException(str);
}
out.println("RCPT TO: " + to);
out.flush();
if (!checkStatus("250")) {
throw new ProtocolException(str);
}
out.println("DATA");
out.flush();
if (!checkStatus("354")) {
throw new ProtocolException(str);
}
out.println("From: " + from);
out.println("To: " + to);
out.println("Subject: " + subject + "n");
out.flush();
out.println("");
out.println(message);
out.println(".");
out.flush();
if (!checkStatus("250")) {
throw new ProtocolException(str);
}
out.println("QUIT");
out.flush();
in.close();
socket.close();
}
private boolean checkStatus(String RFC) throws ProtocolException, IOException {
str = in.readLine();
System.out.println(str);
if (!str.startsWith(RFC)) {
throw new ProtocolException(str);
}
while (str.indexOf('-') == 3) {
str = in.readLine();
if (!str.startsWith("RFC")) {
throw new ProtocolException(str);
}
}
return true;
}
}
Ho effettuato diverse prove, specificando come "FROM" il mio indirizzo di posta e "TO" un altro mio indirizzo di posta, ma se effettuo il "Run on Server", ma il server mi risponde "java.net.ConnectException: Connection refused"
Avete qualche idea? o una guida da linkarmi su come fare?
Grazie in anticipo