Anexar documento no email

4 respostas
S

Olá Pessoal,

Estou utilizando a classe Email para enviar alguns e-mails em java. Gostaria de saber como anexar documentos utilizando essa classe. Por exemplo, anexar um documento word ou excel.

Grato a todos.

Abs

4 Respostas

B

Que classe Email é essa? De que pacote?

R

Verifica se este post te ajuda

S

mail.jar

xandelol
Eu estou usando essa classe aqui pra fazer o envio de email com anexo...
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;

/**
 *
 * @author gs10299
 */
public class TesteEnvioEmailAnexo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        String SMTP_HOST_NAME = "servidor de email";
        String SMTP_PORT = "porta do servidor"; 
	String SMTP_FROM_ADDRESS="email remetente";
	String SMTP_TO_ADDRESS="email destinatario";
	String subject="Assunto";
	String fileAttachment = "Path do arquivo";
	
        Properties props = new Properties();
        
        props.put("mail.smtp.host", "servidor");
	props.put("mail.smtp.auth", "true");
	props.put("debug.servidor", "true");
	props.put("port.servidor", SMTP_PORT );
	Session session = Session.getInstance(props,new javax.mail.Authenticator()
	  	{protected javax.mail.PasswordAuthentication 
	      		getPasswordAuthentication() 
	      	{return new javax.mail.PasswordAuthentication("usuario","senha");}});
        Store store = null;  
            try {
                store = session.getStore("imap");
            } catch (NoSuchProviderException ex) {
                throw new RuntimeException(ex);
            }
            try {
                store.connect("servidor", "usuario", "senha");
            } catch (MessagingException ex) {
                throw new RuntimeException(ex);
            }
        Folder folder = store.getDefaultFolder();
	try{
          Message msg = new MimeMessage(session);
          
          msg.setFrom(new InternetAddress(SMTP_FROM_ADDRESS));
	  // create the message part 
	  MimeBodyPart messageBodyPart = 
	      new MimeBodyPart();
	  //fill message
	  messageBodyPart.setText("Test mail one");
	  Multipart multipart = new MimeMultipart();
	  multipart.addBodyPart(messageBodyPart);
	  // Part two is attachment
	  messageBodyPart = new MimeBodyPart();
	  DataSource source = 
	      new FileDataSource(fileAttachment);
	  messageBodyPart.setDataHandler(
	      new DataHandler(source));
	  messageBodyPart.setFileName(fileAttachment);
	  multipart.addBodyPart(messageBodyPart);
	  // Put parts in message
	  msg.setContent(multipart);
         
	  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SMTP_TO_ADDRESS));

	  msg.setSubject(subject);
	  //msg.setContent(content, "text/plain");

	  Transport.send(msg);
	  System.out.println("Sucesso....................................");
          folder = folder.getFolder("Mensagens Enviadas");  
          folder.open(Folder.READ_WRITE);  
          folder.appendMessages(new Message[] { msg });  
          folder.close(true);
	}
	catch(Exception e){
	      e.printStackTrace();    	
	}
  }
}
Estou usando as API's activation.jar e mail.jar

Agora é só adapta o código à sua lógica.

Criado 24 de abril de 2013
Ultima resposta 25 de abr. de 2013
Respostas 4
Participantes 4