Beh... ne ho appena sviluppata una seconda versione che permette l'invio anche a più destinatari. Ovviamente la classe ha cambiato la firma del metodo, ma è più flessibile.
codice:
import java.io.*;
import java.net.*;
public class SendMail {
static int SMTPport = 25;
static Socket socket;
static DataInputStream in;
static DataOutputStream out;
static PrintStream prout;
public static void sendMail(String mailServer,
String[] recipient,
String[] cc,
String subject,
String messaggio,
String fileName,
String userName,
String from) {
try {
Socket s = new Socket(mailServer, 25);
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#"; // Servirà?? -- Data Separator String --
sendln(in, out, "EHLO " + userName);
sendln(in, out, "MAIL FROM: <"+ from + ">");
for (int i=0; i<recipient.length; i++)
sendln(in, out, "RCPT TO: <" + recipient[i] + ">" );
for (int i=0; i<cc.length; i++)
sendln(in, out, "RCPT CC: <" + cc[i] + ">");
sendln(in, out, "DATA");
sendln(out, "MIME-Version: 1.0");
sendln(out, "Subject: " + subject);
sendln(out, "From: " + userName + " <" + from + ">");
for (int i=0; i<recipient.length; i++)
sendln(out, "To: <" + recipient[i] + ">");
for (int i=0; i<cc.length; i++)
sendln(out, "Cc: <" + cc[i] + ">");
sendln(out, "Content-Type: multipart/mixed; boundary=\"" + boundary +"\"");
sendln(out, "\r\n--" + boundary);
// Send the body
sendln(out, "Content-Type: text/plain; charset=\"us-ascii\"\r\n");
sendln(out, messaggio);
sendln(out, "\r\n--" + boundary );
// send the attachment
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();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void sendln(BufferedReader in, BufferedWriter out, String s) {
try {
out.write(s + "\r\n");
out.flush();
Thread.sleep(1000);
s = in.readLine();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void sendln(BufferedWriter out, String s) {
try {
out.write(s + "\r\n");
out.flush();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Modo d'uso:
codice:
String [] destinatari = {"dest1@host.com",
"dest2@host.com",
... // Altri eventuali destinatari
};
String [] cc = {"dest_cc1@host.com",
"dest_cc2@host.com",
... // Altri destinatari in copia
};
SendMail.sendMail(hostName,
destinatari,
cc,
oggetto,
messaggio,
nomeFile,
nomeUtente,
emailMittente);
Non so perchè, però, i destinatari in copia non ricevono la mail. Forse sbaglio qualcosa nel comando SMTP? A chiunque mi fornisca informazioni utili, un GRAZIE è ben accetto.
Ciao.