Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2001
    Messaggi
    193

    [java] invio mail con allegati

    Ciao ,ho una classe java che invia mail è questo funziona, ma adesso servirebbe anche l'invio di allegati, visto che sono nuova di java ho provato ad aggiungere il codice per l'invio di allegati ma non funziona ve lo posto:

    package it.lispa.rap.service.stato;

    import java.util.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.text.DateFormat;

    public class SendEmail
    {

    private String host;
    private String emailTo;
    private String emailFrom;
    private String messaggio;
    private String oggetto;
    private int port;

    public String getHost( )
    {
    return host;
    }

    public void setHost(String value )
    {
    this.host = value;
    }

    public String getEmailTo( )
    {
    return emailTo;
    }

    public void setEmailTo(String value )
    {
    this.emailTo = value;
    }

    public String getEmailFrom( )
    {
    return emailFrom;
    }

    public void setEmailFrom(String value )
    {
    this.emailFrom = value;
    }

    public String getMessaggio( )
    {
    return messaggio;
    }

    public void setMessaggio(String value )
    {
    this.messaggio = value;
    }

    public void setOggetto(String value )
    {
    this.oggetto = value;
    }

    public void setPort(int value )
    {
    this.port = value;
    }


    public boolean send( ) //throws Exception
    {

    /*
    * se nel file web.xml il primo carattere dell'host è N
    * allora significa che ho disattivato l'invio delle email
    */
    if (this.host.charAt(0)=='N')
    {
    return true;
    }

    Properties props = new Properties();

    props.put("mail.smtp.host", this.host);

    props.put("mail.debug", "false");



    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(false);


    try
    {
    Multipart multipart = new MimeMultipart();
    // create a message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(this.emailFrom));
    InternetAddress[] address = {new InternetAddress(this.emailTo)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(this.oggetto);
    msg.setSentDate(new Date());
    BodyPart messageBodyPart1 = new MimeBodyPart();
    messageBodyPart1.setText(this.messaggio);
    ////Invio allegato
    // crea l'allegato Word

    DataSource source = new FileDataSource( "c:/Documento.doc" );
    BodyPart messageBodyPart2 = new MimeBodyPart();
    msg.setDataHandler( new DataHandler(source) );
    msg.setFileName( "Documento.doc" );

    // aggiunge le parti all'oggetto multipart
    multipart.addBodyPart(messageBodyPart1);
    multipart.addBodyPart( messageBodyPart2 );

    // imposta come contenuto del messaggio l'oggetto multipart
    msg.setContent(multipart);


    ////fine invio allegato
    Transport.send(msg);
    return true;

    }
    catch (MessagingException mex)
    {
    // System.out.println("\n--Exception handling in SendEmail.java");

    mex.printStackTrace();
    Exception ex = mex;
    do
    {
    if (ex instanceof SendFailedException)
    {
    SendFailedException sfex = (SendFailedException)ex;
    Address[] invalid = sfex.getInvalidAddresses();
    if (invalid != null)
    {
    System.out.println(" ** Invalid Addresses");
    if (invalid != null)
    {
    for (int i = 0; i < invalid.length; i++)
    System.out.println(" " + invalid[i]);
    }
    }
    Address[] validUnsent = sfex.getValidUnsentAddresses();
    if (validUnsent != null)
    {
    // System.out.println(" ** ValidUnsent Addresses");
    if (validUnsent != null)
    {
    for (int i = 0; i < validUnsent.length; i++)
    System.out.println(" "+validUnsent[i]);
    }
    }
    Address[] validSent = sfex.getValidSentAddresses();
    if (validSent != null)
    {
    // System.out.println(" ** ValidSent Addresses");
    if (validSent != null)
    {
    for (int i = 0; i < validSent.length; i++)
    System.out.println(" "+validSent[i]);
    }
    }
    }

    if (ex instanceof MessagingException)
    ex = ((MessagingException)ex).getNextException();
    else
    ex = null;
    } while (ex != null);
    return false;
    }

    }

    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2004
    Messaggi
    24
    In allegato un esempio funzionante anche se forse un po' diverso dal tuo.... prova un po' a guardare


    //
    // da http://www.javacommerce.com/articles/sendingmail.htm
    //

    package com.xxx.yyy;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;

    public class SendMail {

    private String smtpServer;
    private String subject;
    private String from;
    private String[] recipients;
    private String message;
    private String[] filename;

    public SendMail() {
    this.smtpServer = "xxx.yyy.zzz.hhh";
    this.subject = "prova";
    this.from = "indirizzo.da@zzzzzzz.it";
    recipients = new String[1];
    this.recipients[0] = "indirizzo.a@zzzzzzz.it";
    this.message = "ciao";
    filename = new String[1];
    this.filename[0] = "f:\\nome-cartella\\nome-file.txt";
    }

    public SendMail(String asmtpServer, String aSubject, String aFrom, String aRecipients[], String aMessage , String aFileName[]) {
    this.smtpServer = asmtpServer;
    this.subject = aSubject;
    this.from = aFrom;
    // Verifico quanti indirizzi not null o vuoti
    int j=0;
    for (int i = 0; i < aRecipients.length; i++) {
    if ((aRecipients[i]!=null) && (!aRecipients[i].trim().equals(""))) {
    j = j + 1;
    }
    }
    recipients = new String[j];
    j = 0;
    for (int i = 0; i < aRecipients.length; i++) {
    if ((aRecipients[i]!=null) && (!aRecipients[i].trim().equals(""))) {
    this.recipients[j] = aRecipients[i];
    j = j + 1;
    }
    }
    if (j==0) {
    recipients = null;
    }
    this.message = aMessage;
    // Verifico quanti file not null o vuoti
    j=0;
    for (int i = 0; i < aFileName.length; i++) {
    if ((aFileName[i]!=null) && (!aFileName[i].trim().equals(""))) {
    j = j + 1;
    }
    }
    filename = new String[j];
    j = 0;
    for (int i = 0; i < aFileName.length; i++) {
    if ((aFileName[i]!=null) && (!aFileName[i].trim().equals(""))) {
    this.filename[j] = aFileName[i];
    j = j + 1;
    }
    }
    }

    public void postMail() throws MessagingException {
    boolean debug = false;

    // Se non ho destinatari esco senza fare niente
    if (recipients == null) {
    return;
    }

    //Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", smtpServer);

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    MimeMessage msg = new MimeMessage(session);

    // set the from address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    // set the to address
    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
    addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // set the Subject
    msg.setSubject(subject);

    // Create Multi-Part container
    Multipart multipart = new MimeMultipart();

    // Create bodyPart
    BodyPart messageBodyPart = new MimeBodyPart();
    // Fill the message in bodyPart
    messageBodyPart.setText(message);
    // Add the bodyPart
    multipart.addBodyPart(messageBodyPart);

    // Reference new bodyPart - atrtachement
    for (int i = 0; i < filename.length; i++) {
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename[i]);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename[i]);
    // Add the second bodyPart - attachement
    multipart.addBodyPart(messageBodyPart);
    }

    // Put parts in message
    msg.setContent(multipart);

    // Send message
    Transport.send(msg);
    }

    }

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2001
    Messaggi
    193

    [java] invio mail con allegati

    Grazie funziona

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.