Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 16

Discussione: invio automatico mail

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    13

    invio automatico mail

    Ciao a tutti, volevo sapere se è possibile inviare un file .html già preimpostato via email.
    Ho gia impostato la connessione al server e invia il tutto, però ora vorrei cambiare il testo da una stringa ad un intero file html. Qualcuno sa aiutarmi?
    Grazie mille
    ciao

  2. #2
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    ti posto una classettina che uso io per le mail.. ci sono tutti i metodi, tra cui anche quelli per l'inoltro di mail html..

    codice:
    /******************************************************/
    /** CLASSE PER LA GESTIONE DELLE MAIL - VERSIONE 7.0 **/
    /******************************************************/
    
    import com.sun.mail.smtp.SMTPMessage;
    import java.util.*;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.Message.RecipientType;
    import javax.mail.internet.*;
    
    public class MailHelper{
    
    	private String smtpServer = "smtp.miosmtp.it";
    
    	/**
    	 * METODO COSTRUTTORE DI DEFAULT
    	 * 
    
    	 */
    	public MailHelper(){
    	}
    
    	/**
    	 * METODO COSTRUTTORE PER ASSEGNAZIONE SMPT
    	 * 
    
    	 * @param smtpServer
    	 * smtpServer da assegnare alla classe
    	 * @return
    	 */
    	public MailHelper(String smtpServer){
    		this.smtpServer = smtpServer;
    	}
    
    
    	
    	/**
    	 * METODO PER INOLTRO MAIL DI TESTO
    	 * 
    
    	 * @param from
    	 * mittente della mail
    	 * @param to
    	 * destinatari della mail
    	 * @param subject
    	 * oggetto della mail
    	 * @param body
    	 * testo semplice non formattato
    	 * @return
    	 * @throws AddressException
    	 * @throws MessagingException
    	 */
    	public boolean sendMail(String from, String[] to, String [] toBCC, String subject, String body) throws AddressException, MessagingException{
    		
    		boolean returnValue = false;
    
    		Properties props = System.getProperties();
    		props.put("mail.smtp.host", smtpServer);
    		Session session = Session.getDefaultInstance(props, null);
    
    		// -- Create a new message --
    		Message msg = new MimeMessage(session);
    
    		// -- Set the FROM and TO fields --
    		msg.setFrom(new InternetAddress(from));
    
    		setTo(to, msg, Message.RecipientType.TO);
    		
    		setTo(toBCC, msg, Message.RecipientType.BCC);
    		
    		// -- Set the subject and body text --
    		msg.setSubject(subject);
    		String mimeEncoding = MimeUtility.mimeCharset("<Java Char encoding name>");
    
    		//msg.setContent(body, "text/html");
    		
    		String charset = "utf-8";
    		msg.setContent(body, "text/plain; charset=" + charset);
    		
    		// -- Set some other header information --
    		msg.setHeader("X-Mailer", "LOTONtechEmail");
    		msg.setSentDate(new Date());
    		
    		// -- Send the message --
    		Transport.send(msg);
    
    		returnValue = true;
    
    		return returnValue;
    		
    	}
    
    
    	/**
    	 * METODO PER INOLTRO MAIL IN FORMATO HTML A PIù DESTINATARI
    	 * 
    
    	 * @param from
    	 * mittente della mail
    	 * @param to
    	 * destinatari della mail
    	 * @param subject
    	 * oggetto della mail
    	 * @param body
    	 * testo formattato HTML
    	 * @return
    	 * @throws MessagingException
    	 */
    	public boolean sendHtmlMail(String from, String[] to, String[] toBCC, String subject, String body) throws MessagingException{
    		
    		boolean returnValue = false;
    
        	Properties props2 = System.getProperties();
            props2.put("mail.smtp.host", smtpServer);
            Session session = Session.getDefaultInstance(props2, null);
            SMTPMessage msg = new SMTPMessage(session);
            msg.setHeader("X-Mailer", "LOTONtechEmail");
            msg.setSentDate(new Date());
            msg.setFrom(new InternetAddress(from));
            msg.setSubmitter(from);
            msg.setSubject(subject);
            
    		setTo(to, msg, Message.RecipientType.TO);
    		
    		setTo(toBCC, msg, Message.RecipientType.BCC);
    
            String charset = "utf-8";
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(body, "text/html; charset=" + charset);
            MimeMultipart multipart = new MimeMultipart("related");
            multipart.addBodyPart(messageBodyPart);
            msg.setContent(multipart);
            Transport.send(msg);
            returnValue = true;
    
            return returnValue;
    	}
    	
    	
    	/**
    	 * METODO PER INOLTRO MAIL SMTP CON PIù ALLEGATI
    	 * 
    
    	 * @param from
    	 * mittente della mail
    	 * @param to
    	 * destinatari della mail
    	 * @param subject
    	 * oggetto della mail
    	 * @param body
    	 * testo semplice non formattato
    	 * @param allegati
    	 * array di stringhe path allegati
    	 * @return
    	 * @throws MessagingException
    	 */
    	public boolean sendSMTPMail(String from, String[] to, String[] toBCC, String subject, String body, String [] allegati) throws MessagingException{
    		boolean returnValue = false;
    
    		String fromName = from;
    
    		Properties props = System.getProperties();
    		props.put("mail.smtp.host", smtpServer);
    		Session session = Session.getDefaultInstance(props, null);
    
    		SMTPMessage msg = new SMTPMessage(session);
    		msg.setHeader("X-Mailer", "LOTONtechEmail");
    		msg.setSentDate(new Date());
    		msg.setFrom(new InternetAddress(fromName));
    		msg.setSubmitter(fromName);
    		msg.setSubject(subject);
    
    		setTo(to, msg, Message.RecipientType.TO);
    		
    		setTo(toBCC, msg, Message.RecipientType.BCC);
    		
    		String charset = "utf-8";
    		BodyPart messageBodyPart = new MimeBodyPart();
    		messageBodyPart.setContent(body, "text/html; charset=" + charset);
    		MimeMultipart multipart = new MimeMultipart("related");
    		multipart.addBodyPart(messageBodyPart);
    		
    		// ciclo per ogli allegato
    		for(int i = 0;i<allegati.length;i++){
    			
    	        if (!allegati[i].equals("")){
    	            messageBodyPart = new MimeBodyPart();
    	            DataSource fds = new FileDataSource(allegati[i]);
    	            messageBodyPart.setFileName(fds.getName());
    	            messageBodyPart.setDataHandler(new DataHandler(fds));
    	            messageBodyPart.setHeader("Content-ID", "<memememe>");
    	            multipart.addBodyPart(messageBodyPart);
    	        }
    			
    		} // fine ciclo
            
    		msg.setContent(multipart);
    		Transport.send(msg);
    		returnValue = true;
    
    		return returnValue;
    	}	
    	
    	/**
    	 * METODO PRIVATO PER CONTROLLI E ASSEGNAZIONI CAMPI DESTINATARIO
    	 * 
    
    	 * @param to
    	 * destinatari della mail
    	 * @param msg
    	 * messaggio a cui aggiungere i destinatari
    	 * @param rt
    	 * tipo di destinatario
    	 * @throws MessagingException
    	 */
    	private void setTo(String[] to, Message msg, RecipientType rt) throws MessagingException{
    		if(to != null){
    			boolean addTo = false;
    			InternetAddress[] addressTo = new InternetAddress[to.length]; 
    			for (int i = 0; i < to.length; i++)
    			{
    				if(to[i]!=null && !"".equals(to[i])){
    					addressTo[i] = new InternetAddress(to[i]);
    					addTo = true;
    				}
    			}
    			if(addTo){
    				msg.setRecipients(rt, addressTo);
    			}
    		}
    	}
    	
    }
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    13
    grazie x l'immediatezza
    da quello che vedo salvi in una stringa "body" il testo preformattato html...ma poi dove glielo passi per poterlo spedire?

  4. #4
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Originariamente inviato da Riky86
    grazie x l'immediatezza
    da quello che vedo salvi in una stringa "body" il testo preformattato html...ma poi dove glielo passi per poterlo spedire?
    Nel metodo sendHtmlMail:
    codice:
    messageBodyPart.setContent(body, "text/html; charset=" + charset);
    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. #5
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    la spedizione effettiva la fanno le mail di import javax.mail.*;
    devi cercarti ed importarti i jar nel tuo progetto

    tu prepari solo le stringhe da passare, istanzi la classe che ti ho dato e chiami il metodo sendHtmlMail() con tutti i tuoi parametri..
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    13
    anzi cambio di idea, quello l'ho capito!! mi rimane da scoprire come salvi il testo html dentro alla String body, grazie

  7. #7
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    esattamente come se stessi scrivendo una pagina html

    codice:
    String body = "<html><head></head><body>testo di prova
    ciao</body></html>"
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  8. #8
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    13
    ahhhhhhh....ma quindi non posso salvare dentro alla stringa una pagina html gia fatta? ad esempio

    String body = nuovamail.html

  9. #9
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Originariamente inviato da Riky86
    ahhhhhhh....ma quindi non posso salvare dentro alla stringa una pagina html gia fatta? ad esempio

    String body = nuovamail.html
    Certo... leggi il file riga per riga e costruisci una stringa con tutto il suo contenuto.

    codice:
    String tuoBody = "";
    BufferedReader br = new BufferedReader( new FileReader("nuovamail.html") );
    String linea = "";
    while((linea = br.readLine()) != null) {
       tuoBody += linea + "\n";
    }
    br.close();
    Con le dovute accortezze per le eccezioni.

    A questo punto passi al metodo la variabile "tuoBody".


    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

  10. #10
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    Originariamente inviato da Riky86
    ahhhhhhh....ma quindi non posso salvare dentro alla stringa una pagina html gia fatta? ad esempio

    String body = nuovamail.html
    ahhh ok ero io che non capivo !!
    si come ti ha detto lele cmq..
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

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.