codice:
public static void sendMail(String mailServer, String[] recipient, String[] cc, String subject, String[] messaggio, String fileName, String userName, String password, String nomeUtente, String from, String nomeFileVis, String confLettura, boolean requireLogin, boolean confReceipt) { String linea = ""; boolean autenticated = false; authMechanism = new boolean[3]; authMechanism[0] = false; // CRAM-MD5 authMechanism[1] = false; // LOGIN authMechanism[2] = false; // PLAIN 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") ); linea = in.readLine(); String boundary = "Dat_Sep_Str_#COD#"; // -- Data Separator String -- sendln(in, out, "EHLO " + InetAddress.getLocalHost().getHostName()); // + userName recuperaMechanism( in ); if ( requireLogin ) { for(int i=0; i<authMechanism.length; i++) { if ( authMechanism[i] && !autenticated ) { autenticated = true; sendLogin(0, in, out, userName, password, i); } } if ( !autenticated ) { System.out.println("Autenticazione non riuscita: nessun metodo di autenticazione rilevato o metodo non supportato!"); } } sendln(in, out, "MAIL FROM: <"+ InetAddress.getLocalHost().getHostName() + ">"); for (int i=0; i<recipient.length; i++) sendln(in, out, "RCPT TO: <" + recipient[i] + ">" + (confReceipt ? " NOTIFY=SUCCESS" : "") ); for (int i=0; i<cc.length; i++) sendln(in, out, "RCPT TO: <" + 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, "Disposition-Notification-To: "+confLettura); sendln(out, "X-Confirm-Reading-To: "+confLettura); 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"); for (int i=0; i<messaggio.length; i++) sendln(out, messaggio[i]); sendln(out, "\r\n--" + boundary ); // send the attachment String nomeFile = (new File(nomeFileVis)).getName(); sendln(out, "Content-Type:Application/Octet-stream; name="+nomeFile); sendln(out, "Content-Disposition: attachment;filename=\""+nomeFile+"\""); sendln(out, "Content-transfer-encoding: base64\r\n"); //MIMEBase64.encode(fileName, out); sendln(out, "\r\n\r\n--" + boundary + "--\r\n"); sendln(in, out,"."); sendln(in, out, "QUIT"); s.close(); } catch (Exception e) { e.printStackTrace(); } }
In pratica, send mail instanzia Socket s = new Socket(mailServer, 25);