Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    261

    E-mail

    Ragazzi sto cecando di creare un messaggio che invia un e-mail in java questo è il codice ...ma nn so perche nn funziona forse è l'autetificazione vi posto il codice potreste darmi una mano a renderlo funzionate.....vi rigrazio


    import java.util.*;
    import java.net.*;
    import java.io.*;

    public class InvioMail
    {
    static BufferedReader in;
    static PrintWriter out;
    static String server="smtp.fastwebnet.it";
    static String mittente="aspire1982@hotmail.it";
    static String destinatario="aspire1982@hotmail.it";
    static String messaggio="ciao";

    public static void main(String[] args)
    {
    try
    {
    Socket s = new Socket(server, 25);
    out = new PrintWriter(s.getOutputStream());
    in = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String hostName = InetAddress.getLocalHost().getHostName();

    receive();
    send("HELO "+ hostName);
    receive();
    send("MAIL FROM: <"+ mittente + ">");
    receive();
    send("RCPT TO: <" + destinatario + ">");
    receive();
    send("DATA");
    receive();
    StringTokenizer tokenizer = new StringTokenizer(messaggio, "\n");
    while (tokenizer.hasMoreTokens())
    send(tokenizer.nextToken());
    send(".");
    receive();
    s.close();
    }
    catch (IOException exception)
    {
    System.out.println("Error: " + exception);
    }
    }

    public static void send(String s) throws IOException
    {
    System.out.println(s);
    System.out.println("\n");
    out.print(s);
    out.print("\r\n");
    out.flush();
    }

    public static void receive() throws IOException
    {
    String line = in.readLine();
    if (line != null)
    {
    System.out.println(line);
    System.out.println("\n");
    }
    }
    }



    e cmq il tipo di errore che da è

    init:
    deps-jar:
    compile-single:
    run-single:
    220 aa012msr.fastwebnet.it ESMTP Service ready


    HELO acer-2d60536d59


    250 aa012msr.fastwebnet.it


    MAIL FROM: <aspire1982@hotmail.it>


    250 MAIL FROM:<aspire1982@hotmail.it> OK


    RCPT TO: <aspire1982@hotmail.it>


    250 RCPT TO:<aspire1982@hotmail.it> OK


    DATA


    354 Start mail input; end with <CRLF>.<CRLF>


    ciao


    .


    554 Message refused

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    consiglio spassionato: usa il framework javamail... se non puoi o non vuoi (boh?) dai un'occhiata alla pillola di LeleFT su java ed email. Il tuo problema quasi sicuramente risiede nel fatto che il tuo smtp si aspetta un'autenticazione, cosa che non avviene.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,326
    Esatto. Se il tuo server SMTP richiede l'autenticazione la devi implementare tramite i comandi AUTH e ti garantisco che non è una cosa semplice (io ho implementato i metodi di autenticazione PLAIN, LOGIN e CRAM-MD5).

    PS: Correggo il titolo.
    PPS: La prossima volta posta il codice all'interno dei tag [ CODE ] e [ /CODE ], utilizzando anche l'indentazione dello stesso.

    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

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    261
    Ragazzi mi sn copiato queste classi per l'invio di e-mail

    http://forum.html.it/forum/showthrea...readid=1150691

    questa è la classe MIMIBase64 l'ho trovata su internet :

    codice:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    
    /**
     * Class declaration
     *
     *
     * @author Johannes Plachy
     * @version %I%, %G%
     */
    public class MIMEBase64
    {
    
       /*
        * Base64 uses a 65 character subset of US-ASCII,
        * allowing 6 bits for each character so the character
        * "m" with a Base64 value of 38, when represented
        * in binary form, is 100110.
        *
        * With a text string, let's say "men" is encoded this
        * is what happens :
        *
        * The text string is converted into its US-ASCII value.
        *
        * The character "m" has the decimal value of 109
        * The character "e" has the decimal value of 101
        * The character "n" has the decimal value of 110
        *
        * When converted to binary the string looks like this :
        *
        * m   01101101
        * e   01100101
        * n   01101110
        *
        * These three "8-bits" are concatenated to make a
        * 24 bit stream
        * 011011010110010101101110
        *
        * This 24 bit stream is then split up into 4 6-bit
        * sections
        * 011011 010110 010101 101110
        *
        * We now have 4 values. These binary values are
        * converted to decimal form
        * 27     22     21     46
        *
        * And the corresponding Base64 character are :
        * b      W       V     u
        *
        * The encoding is always on a three characters basis
        * (to have a set of 4 Base64 characters). To encode one
        * or two then, we use the special character "=" to pad
        * until 4 base64 characters is reached.
        *
        * ex. encode "me"
        *
        * 01101101  01100101
        * 0110110101100101
        * 011011 010110 0101
        * 111111    (AND to fill the missing bits)
        * 011011 010110 010100
        * b     W      U
        * b     W      U     =  ("=" is the padding character)
        *
        * so "bWU="  is the base64 equivalent.
        *
        * encode "m"
        *
        * 01101101
        * 011011 01
        * 111111         (AND to fill the missing bits)
        * 011011 010000
        * b     Q     =  =   (two paddings are added)
        *
        * Finally, MIME specifies that lines are 76 characters wide maximum.
        *
        */
    
       static String  BaseTable[] =
       {
          "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
       };
    
       /**
        * Method declaration
        *
        *
        * @param filename
        *
        * @param out
        *
        *
        */
       public static void encode( String filename, BufferedWriter out )
       {
          try
          {
             File              f = new File( filename );
             FileInputStream   fin = new FileInputStream( filename );
    
             // read the entire file into the byte array
    
             byte              bytes[] = new byte[ ( int )( f.length() ) ];
             int               n = fin.read( bytes );
    
             if ( n < 1 )
             {
                return;                       // no bytes to encode!?!
             }
    
             byte  buf[] = new byte[ 4 ];     // array of base64 characters
             int   n3byt = n / 3;             // how 3 bytes groups?
             int   nrest = n % 3;             // the remaining bytes from the grouping
             int   k = n3byt * 3;             // we are doing 3 bytes at a time
             int   linelength = 0;            // current linelength
             int   i = 0;                     // index
    
             // do the 3-bytes groups ...
    
             while ( i < k )
             {
                buf[ 0 ] = ( byte )( ( bytes[ i ] & 0xFC ) >> 2 );
                buf[ 1 ] = ( byte )( ( ( bytes[ i ] & 0x03 ) << 4 ) | ( ( bytes[ i + 1 ] & 0xF0 ) >> 4 ) );
                buf[ 2 ] = ( byte )( ( ( bytes[ i + 1 ] & 0x0F ) << 2 ) | ( ( bytes[ i + 2 ] & 0xC0 ) >> 6 ) );
                buf[ 3 ] = ( byte )( bytes[ i + 2 ] & 0x3F );
    
                send( out, BaseTable[ buf[ 0 ] ] );
                send( out, BaseTable[ buf[ 1 ] ] );
                send( out, BaseTable[ buf[ 2 ] ] );
                send( out, BaseTable[ buf[ 3 ] ] );
    
                /*
                 * The above code can be written in more "optimized"
                 * way. Harder to understand but more compact.
                 * Thanks to J. Tordera for the tip!
                 * buf[0]= (byte)(b[i] >> 2);
                 * buf[1]= (byte)(((b[i] & 0x03)  << 4)|(b[i+1]>> 4));
                 * buf[2]= (byte)(((b[i+1] & 0x0F)<< 2)|(b[i+2]>> 6));
                 * buf[3]= (byte)(b[i+2] & 0x3F);
                 * send(out,BaseTable[buf[0]]+BaseTable[buf[1]]+
                 * BaseTable[buf[2]]+BaseTable[buf[3]]);
                 */
    
                if ( ( linelength += 4 ) >= 76 )
                {
                   send( out, "\r\n" );
    
                   linelength = 0;
                }
    
                i += 3;
             }
    
             // deals with with the padding ...
    
             if ( nrest == 2 )
             {
    
                // 2 bytes left
    
                buf[ 0 ] = ( byte )( ( bytes[ k ] & 0xFC ) >> 2 );
                buf[ 1 ] = ( byte )( ( ( bytes[ k ] & 0x03 ) << 4 ) | ( ( bytes[ k + 1 ] & 0xF0 ) >> 4 ) );
                buf[ 2 ] = ( byte )( ( bytes[ k + 1 ] & 0x0F ) << 2 );
             }
             else if ( nrest == 1 )
             {
    
                // 1 byte left
    
                buf[ 0 ] = ( byte )( ( bytes[ k ] & 0xFC ) >> 2 );
                buf[ 1 ] = ( byte )( ( bytes[ k ] & 0x03 ) << 4 );
             }
    
             if ( nrest > 0 )
             {
    
                // send the padding
    
                if ( ( linelength += 4 ) >= 76 )
                {
                   send( out, "\r\n" );
                }
    
                send( out, BaseTable[ buf[ 0 ] ] );
                send( out, BaseTable[ buf[ 1 ] ] );
    
                // Thanks to R. Claerman for the bug fix here!
    
                if ( nrest == 2 )
                {
                   send( out, BaseTable[ buf[ 2 ] ] );
                }
                else
                {
                   send( out, "=" );
                }
    
                send( out, "=" );
             }
    
             out.flush();
          }
          catch ( Exception e )
          {
             e.printStackTrace();
          }
       }
    
    
       /**
        * Method declaration
        *
        *
        * @param out
        *
        * @param s
        *
        *
        */
       public static void send( BufferedWriter out, String s )
       {
          try
          {
             out.write( s );
             System.out.print( s );
          }
          catch ( Exception e )
          {
             e.printStackTrace();
          }
       }
    
    
    }

    il runtime funziona ma l'e-mail non la invia ..............

    come paramentri di invio ho messo........date un occhiata

    codice:
    String mailServer = "smtp.fastweb.it"; //il mio gestore è fastweb 
    
    String[] recipient {"aspire1982@hotmail.it"};  destinatario
    
    String[] cc = new String[0];   // Non necessito di destinatari in copia
    
    String subject = "Messaggio di prova";   // L'oggetto della mail
    
    String[] messaggio = {"Ciao Pippo!", "Come va?", "Questa è una mail di prova."};
    
    String fileName = "C:\\Allegato.zip";   // Allego un file ZIP
    
    String userName = "******";   // metto la mia user di libero
    
    String password = "*******";   // metto la mia password di libero
    
    String nomeUtente = "Azienda";   // Il nome che verrà visualizzato al destinatario
    
    String from = "aspire1982@libero.it";  mia e-mail di libero
    
    String nomeFileVis = "Ordine.zip";   // Il destinatario vedrà un allegato che si chiama "Ordine.zip"
    String confLettura = "info@miodominio.it";   // Voglio la conferma di lettura
    boolean requireLogin = false;   // Non necessito di login
    boolean confReceipt = false;   // Non mi interessa la notifica di recapito
    
    // Ora invio la mail:
    SendMail.sendMail(mailServer, recipient, cc, subject, messaggio, fileName, userName, password, nomeUtente, from, nomeFileVis, confLettura, confReceipt);

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Originariamente inviato da Andrea1979
    consiglio spassionato: usa il framework javamail... se non puoi o non vuoi (boh?) dai un'occhiata alla pillola di LeleFT su java ed email. Il tuo problema quasi sicuramente risiede nel fatto che il tuo smtp si aspetta un'autenticazione, cosa che non avviene.
    Mi autoquoto: sottovaluti il problema dell'autenticazione. E come ti spiegava anche LeleFT (una vera e propria autorità in campo java), non è cosa banale. Non reinventare la ruota:

    JavaMail: http://java.sun.com/products/javamail/index.jsp
    Activation Framework: http://java.sun.com/products/javabeans/jaf/index.jsp

    E negli esempi che trovi al primo link c'è anche tutta la menata dell'autenticazione.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  6. #6
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    261
    guarda che infatti è la pillola di left dove c'è anche l'autentificazione metto le mie password e user ma nn invia!!..........percio ho chiesto di dare un'occhiata

  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    tu HAI bisogno di autenticazione: era l'esempio sotto nella pillola di Lele.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  8. #8
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    261
    Lele o left ?

  9. #9
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    FT
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  10. #10
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    261
    Si ma infatti in quella di left c'è anche l'autenticazione e quando chiami il metodo

    SendMail()

    dovresti mettere anche le password e user ...io le ho messe ma al destinatario nn arriva niente...percio ho chiesto di dare un'occhiata.........

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.