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
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
Que classe Email é essa? De que pacote?
Verifica se este post te ajuda
mail.jar
Eu estou usando essa classe aqui pra fazer o envio de email com anexo…
[code]
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();
}
}
}
[/code]Estou usando as API’s activation.jar e mail.jar
Agora é só adapta o código à sua lógica.