Pagina 2 di 4 primaprima 1 2 3 4 ultimoultimo
Visualizzazione dei risultati da 11 a 20 su 33
  1. #11
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Lele! Ho appena provato a mandarmi una email su interfree e il tuo codice funziona quasi alla meraviglia.. nel senso che l'allegato (un file di testo) arriva col nome "storpiato": da Ciao.txt a CCiao.txt
    Ci si può pure vivere... adesso provo con un file di natura diversa.

    [edit]
    e pure un mp3 ha viaggiato sano e salvo fino a destinazione
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  2. #12
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    A me non ha dato questa cosa... controlla di non aver inserito, per sbaglio, una lettera C nella riga dove costruisco il nome del file visualizzato... (anche perchè lo faccio costruire a Java il nome del file, partendo dal percorso del file gli faccio recuperare il Nome, senza tutto il path...).


    PS: io ho inviato un file Excel ed è arrivato perfetto.

    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  3. #13
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    ah... per un qualche motivo, nel nome del file le barre (dritte o rovesce) e i ":" vengono eleminate... la C era del C:/

    No problem, mui bueno lo stesso.

    [EDIT]
    Potrei permettermi di suggerire di farne una pillola? La cosa è sicuramente interessante, visto che consente a tutti di mandare email più o meno complesse via Java senza l'ausilio di librerie di terze parti o di client di posta. Just my 2/100 of course.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #14
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    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.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  5. #15
    Utente di HTML.it L'avatar di Mods
    Registrato dal
    Jun 2004
    Messaggi
    302
    ciao! Io sto presentando la tesi di maturità riguardo la posta elettronica proprio in questo periodo.
    Se stai usando libero per quel ke ti posso dire ho testato personalmente ke "rcpt cc:" non è riconosciuto.
    Ho guardato l'help del server libero con telnet facendo "help rcpt" però non dà informazioni riguardo al CC ma solo a come aggiungerli al TO
    Ci sono 10 tipi di persone al mondo: quelli che conoscono il codice binario, e quelli che non lo conoscono!

  6. #16
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Va tutto in RCPT TO:
    E' il server che se la sbriga a dividere in base al resto degli headers:

    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[] Bcc,
                                 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, "HELO " + 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 TO:<" + cc[i] + ">");
        }
        for (int i=0; i<Bcc.length; i++) {
          sendln(in, out, "RCPT TO:<" + Bcc[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] + ">");
        }
        for (int i=0; i<Bcc.length; i++) {
          sendln(out, "Bcc: <" + Bcc[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();
        }
       }
     }
    Ho aggiunto anche il vettore dei Bcc
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  7. #17
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Avevo già risolto ieri pomeriggio, mettendo tutto nel TO, ma non ho avuto tempo per le correzioni. Comunque, avevo pensato anch'io ad aggiungere la possibilità di inserire i Bcc, ma il tempo è poco e il lavoro non può aspettare i miei comodi


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  8. #18
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Ultimo aggiornamento: avevo introdotto un ritardo inutile.
    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[] Bcc,
                                 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, "HELO " + 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 TO:<" + cc[i] + ">");
        }
        for (int i=0; i<Bcc.length; i++) {
          sendln(in, out, "RCPT TO:<" + Bcc[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] + ">");
        }
        for (int i=0; i<Bcc.length; i++) {
          sendln(out, "Bcc: <" + Bcc[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();
        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();
        }
       }
     }
    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  9. #19
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    ultimo dettaglio, così mi si risolve anche il problema del nome dell'allegato (ho commentato la riga incriminata e fatto la sostituzione nella riga sottostante)

    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[] Bcc,
                                 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, "HELO " + 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 TO:<" + cc[i] + ">");
        }
        for (int i=0; i<Bcc.length; i++) {
          sendln(in, out, "RCPT TO:<" + Bcc[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] + ">");
        }
        for (int i=0; i<Bcc.length; i++) {
          sendln(out, "Bcc: <" + Bcc[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();
        System.out.println(nomeFile);
        sendln(out, "Content-Type:image/gif; name="+nomeFile);
        //sendln(out, "Content-Disposition: attachment;filename=\""+fileName+"\"");
        sendln(out, "Content-Disposition: attachment;filename=\""+nomeFile+"\"");
        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();
        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();
        }
       }
     }
    Grazie ancora Lele per il tweaking del codice
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  10. #20
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Il problema è che così, se il file da allegare non è nella stessa cartella dell'applicazione, non lo allega perchè non lo trova... o sbaglio?

    Comunque, ho apportato un'altra modifica, così il messaggio può anche essere composto da più righe:
    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 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, "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] + "\n");
             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();
             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();
          }
       }
    }
    Ora il parametro messaggio + un array con tutte le righe del messaggio.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.