Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    147

    [java] Invio mail con allegato

    Ciao,
    devo inviare una mail con un allegato, utilizzando l'oggetto Message,
    imposto la proprietà

    msg.setFileName(nomefile)

    ma il mio obiettivo è recuperare il file che si trova in una cartella specifica, mentre setfilename vuole solamente il nome del file.

    Come fare?

  2. #2
    Prova questo codice:

    codice:
         public void send(String filename){
          Properties props=new Properties();
          props.put("mail.smtp.host",serverSMTP);
    
          Session session = Session.getDefaultInstance(props, null);
          try	{
            Message msg=new MimeMessage(session);
            msg.setFrom(new InternetAddress(emailFrom));
            InternetAddress[] address = {new InternetAddress(emailTo)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
    
    
            Transport transport=session.getTransport(address[0]);
            transport.connect();
            //crea la prima parte
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);
    
            // create the second message part
            MimeBodyPart mbp2 = new MimeBodyPart();
    
            // attach the file to the message
            FileDataSource fds = new FileDataSource(filename);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
    
            // create the Multipart and its parts to it
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);
            // add the Multipart to the message
    
            msg.setContent(mp);
            transport.sendMessage(msg,address);
    
            }catch (Exception ex)	{ System.out.println("errore "  +ex.getMessage());}
    
    }

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    147
    Funziona, Grazie


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.