Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910

    [JAVA] Inviare email con JavaMail

    HO provato questo codice preso da questo link

    codice:
    /* esempio di codice */
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    
    public class SimpleMailAttach {
    
    public static void main (String args[]) throws Exception {
        String smtpHost = args[0];
        String from = args[1];
        String to = args[2];
        String filename = args[3];
    
        // Get system properties
        Properties props = System.getProperties();
    
        // Setup mail server
        props.put("smtp.mail.it", smtpHost);
    
        // Get session
        Session session = Session.getDefaultInstance(props, null);
    
        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(to));
        message.setSubject("Hello this is a mail message");
    
        // Create the multi-part
        Multipart multipart = new MimeMultipart();
    
        // Create part one
        BodyPart messageBodyPart = new MimeBodyPart();
    
        // Fill the message
        messageBodyPart.setText("Here's the file");
    
        // Add the first part
        multipart.addBodyPart(messageBodyPart);
    
        // Part two is attachment 
    
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
    
        // Add the second part
        multipart.addBodyPart(messageBodyPart);
    
        // Put parts in message
        message.setContent(multipart);
    
        // Send message
        Transport.send(message);
      }
    }
    Compilo...passo i parametri cosi:
    java SimpleMailAttach smtp.email.it prima@email.it seconda@email.it file_da_allegare

    Non ricevo nessun errore, niente di niente, ma non ricevo neanche nessuna email...
    Che ne dite?
    GRAZIE 1000

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    logicamente ho scaricato JavaMail e Jaf

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    uppettino

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    nessuno?

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    codice:
    import javax.mail.*;
    import java.util.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    
    public class javamaildemo {
      String host = "..."; //tuo smtp
      String from = "..."; //tuo indirizzo email
      String to = "..."; //destinatario
      String filename = "..."; //percorso-nome file da allegare
    
      public javamaildemo() {
        try {
          // Get system properties
          Properties props = System.getProperties();
    
          // Setup mail server
          props.put("mail.smtp.host", host);
    
          // Get session
          Session session = Session.getDefaultInstance(props, null);
    
          // Define message
          MimeMessage message = new MimeMessage(session);
          message.setFrom(new InternetAddress(from));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setSubject("Ciao da Javamail");
    
          BodyPart messageBodyPart = new MimeBodyPart();
          messageBodyPart.setText("Prova di invio allegato");
          Multipart multipart = new MimeMultipart();
          multipart.addBodyPart(messageBodyPart);
        
        // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
    
    // Put parts in message
        message.setContent(multipart);
    
    
    
          // Send message
          Transport.send(message);
        }
        catch (Exception e) {
          System.out.println(e.toString());
        }
      }
    
      public static void main(String[] args) {
        javamaildemo jv = new javamaildemo();
      }
    }
    Questo funziona (lo uso), controlla la tua riga
    codice:
    props.put(....)
    Il codice che ho postato sopra l'ho preso dall'articolo

    http://java.sun.com/developer/onlineTraining/JavaMail/

    Ciao
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  6. #6
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    Originariamente inviato da Andrea1979
    codice:
    import javax.mail.*;
    import java.util.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    
    public class javamaildemo {
      String host = "..."; //tuo smtp
      String from = "..."; //tuo indirizzo email
      String to = "..."; //destinatario
      String filename = "..."; //percorso-nome file da allegare
    
      public javamaildemo() {
        try {
          // Get system properties
          Properties props = System.getProperties();
    
          // Setup mail server
          props.put("mail.smtp.host", host);
    
          // Get session
          Session session = Session.getDefaultInstance(props, null);
    
          // Define message
          MimeMessage message = new MimeMessage(session);
          message.setFrom(new InternetAddress(from));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setSubject("Ciao da Javamail");
    
          BodyPart messageBodyPart = new MimeBodyPart();
          messageBodyPart.setText("Prova di invio allegato");
          Multipart multipart = new MimeMultipart();
          multipart.addBodyPart(messageBodyPart);
        
        // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
    
    // Put parts in message
        message.setContent(multipart);
    
    
    
          // Send message
          Transport.send(message);
        }
        catch (Exception e) {
          System.out.println(e.toString());
        }
      }
    
      public static void main(String[] args) {
        javamaildemo jv = new javamaildemo();
      }
    }
    Questo funziona (lo uso), controlla la tua riga
    codice:
    props.put(....)
    Il codice che ho postato sopra l'ho preso dall'articolo

    http://java.sun.com/developer/onlineTraining/JavaMail/

    Ciao
    Ti ringrazio andrea ora lo provo, infatti non riesco a capire cosa devo mettere su quella riga,
    se nella variabile host metto il mio server smtp quel smtp.email.it (props.put(...))cosa è?lo devo cambiare?
    grazie mille

  7. #7
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    mi da errore
    codice:
    javax.mail.SendFailedException: Invalid Addresses;
      nested exception is:
            class com.sun.mail.smtp.SMTPAddressFailedException: 553 <damiano85@email.it>: Sender address rejected: not logged in as owner
    Mi sa che richiede l'autenticazione...
    come posso fare?
    grazie ancora

  8. #8
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    mah sembra che ho risolto, ho messo il server smtp in quella riga props.put e tutto è andato per il meglio, l'email è arrivata subito con l'allegato...
    ma cmq non capisco una cosa perchè lo devo ripetere il server smtp??
    Grazie ancora

  9. #9
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    l'errore stava nel nome della proprietà di sistema, credo

    tu cercavi di settare la proprietà smtp.mail.it al valore smtpHost

    Nel codice che ho postato io invece, settano la proprietà mail.smtp.host al valore passato.

    Per quanto riguarda gli errori con gli smtp e l'eventuale richiesta di autenticazione, sempre al link, c'è un esempio con autenticazione: non ne ho avuto bisogno e non ho esplorato ulteriormente, visto che il server che uso non richiede autenticazione.
    Ciao
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  10. #10
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    ok, comunque io in quel comando props.put gli ho passato due host uguali...
    cioè:
    props.put("smtp.email.it", host);
    dove anche la variabile host ha lo stesso valore quindi "smtp.email.it"
    cosi facendo l'email viene inviata, cmq non ho capito bene ancora il suo funzionamento.
    Anche email.it dovrebbe richiedere l'autenticazione, ma sembra andare...boh,
    cmq nel link che hai postato non riesco a trovare l'esempio con l'autenticazione, puoi indicarmelo?
    grazie per il tuo aiuto

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 © 2024 vBulletin Solutions, Inc. All rights reserved.