Home| New Wiki | | Login | User registry | Home Tree PDF
Envio de Correos con adjuntos
Owner:csilva, Version: 6, Date:Mon 29, November 2004,

Se pueden enviar correos electronicos usando Java.

En este ejemplo se envia un correo con HTML y links inmersos en el html (en este caso un archivo .zip). La estructura MIME es la siguiente:

  • mainMultipart "multipart/alternative"
    • opcionalTextPart (text/plain)
    • compoundPart "multipart/related"
      • htmlPart (text/html)
      • imgPart (Content-ID=b.zip)
      • imgPart (Content-ID=i.jpg)

 package net.mapi;
 import java.io.*;
 import java.util.*;
 import javax.mail.*;
 import javax.mail.internet.*;
 import javax.activation.*;
 
 class SendMail {
   static protected String host="smtp_HOST";
   static protected String user="csilva@site";
   static protected String pass="pass";
 
   public static void main(String args[]) 
          throws MessagingException, UnsupportedEncodingException{
   
     Properties props = new Properties();
     Session session = Session.getDefaultInstance(props, null);
     MimeMessage message = new MimeMessage(session);
     message.setSubject("Test with embedded HTML");
     
     Address address = new InternetAddress("yo@yo.com", "Yo");
     message.setFrom(address);
     
     Address toAddress = new InternetAddress("csilva@site.cl");
     message.addRecipient(Message.RecipientType.TO, toAddress);
 
 
     Multipart mainMultipart = new MimeMultipart("alternative");
 
       BodyPart opcionalTextPart = new MimeBodyPart();
       opcionalTextPart.setText("Hola en formato text");
       
       BodyPart htmlPart = new MimeBodyPart();
         String htmlText = "<H1>Hello</H1><img src="cid:i.jpg">"
           +" Aqui <b>esta!</b><br><a href="cid:b.zip">archivo </a>!.";    
         htmlPart.setContent(htmlText, "text/html");          
       
 
       String filename="/i.jpg";
       BodyPart imgPart = new MimeBodyPart();
         DataSource source = new FileDataSource(filename);
         imgPart.setDataHandler(new DataHandler(source));
         imgPart.setFileName(filename);
         imgPart.setHeader("Content-ID","i.jpg");
 
 
       filename="/b.zip";
       BodyPart filePart = new MimeBodyPart();
         source = new FileDataSource(filename);
         filePart.setDataHandler(new DataHandler(source));
         filePart.setFileName(filename);
         filePart.setHeader("Content-ID","b.zip");        
 
       BodyPart compoundPart = new MimeBodyPart();
         Multipart relatedMultiPart = new MimeMultipart("related");
         relatedMultiPart.addBodyPart(htmlPart);
         relatedMultiPart.addBodyPart(imgPart);
         relatedMultiPart.addBodyPart(filePart);  
         compoundPart.setContent(relatedMultiPart);
 
     mainMultipart.addBodyPart(opcionalTextPart);      
     mainMultipart.addBodyPart(compoundPart);
 
     message.setContent(mainMultipart);
     message.saveChanges(); // implicit with send()
     Transport transport = session.getTransport("smtp");
     transport.connect(host, user, pass);
     transport.sendMessage(message, message.getAllRecipients());
     transport.close();
   }
 }


Edit - History - Extract PDF - Extract Tree as PDF

Last Modified

Thu, Nov 19 Sat, Oct 3 Fri, Sep 25 Mon, Aug 3 Mon, Apr 27 Sat, Mar 28 Mon, Jan 19 Tue, Jan 6

Home| New Wiki