Created
May 23, 2019 17:11
-
-
Save nosrednawall/3b88f4f79d745bf7fca41455803914ea to your computer and use it in GitHub Desktop.
lista, compacta e envia arquivos via e-mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Properties; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipOutputStream; | |
import javax.activation.DataHandler; | |
import javax.activation.DataSource; | |
import javax.activation.FileDataSource; | |
import javax.mail.Address; | |
import javax.mail.BodyPart; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
import javax.mail.PasswordAuthentication; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeBodyPart; | |
import javax.mail.internet.MimeMessage; | |
import javax.mail.internet.MimeMultipart; | |
public class JavaMailApp { | |
public static void main(String[] args) throws IOException { | |
Properties props = new Properties(); | |
/** Parâmetros de conexão com servidor Gmail */ | |
props.put("mail.smtp.host", "smtp.office365.com"); | |
props.put("mail.smtp.starttls.enable", "true"); | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.port", "587"); | |
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication("seu endereco de email", "sua senha"); | |
} | |
}); | |
/** Ativa Debug para sessão */ | |
session.setDebug(true); | |
try { | |
Message message = new MimeMessage(session); | |
// Remetente | |
message.setFrom(new InternetAddress("seu endereco de email")); | |
// Destinatário(s) | |
Address[] toUser = InternetAddress.parse("[email protected], [email protected]"); | |
message.setRecipients(Message.RecipientType.TO, toUser); | |
message.setSubject("Teste de java-email");// Assunto | |
String[] array = listaArquivos("/home/anderson/Imagens/imagens_teste/"); | |
try { | |
compactarParaZip("/home/anderson/Imagens/imagens.zip", array); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
message.setContent(setAttachment(message, "imagens", ".zip", "/home/anderson/Imagens/imagens.zip", 1)); | |
/** Método para enviar a mensagem criada */ | |
Transport.send(message); | |
System.out.println("Feito!!!"); | |
} catch (MessagingException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static Multipart setAttachment(Message message, String filename, String extensao, String path, int number) | |
throws MessagingException { | |
Multipart multipart = new MimeMultipart(); | |
BodyPart messageBodyPart = new MimeBodyPart(); | |
for (int i = 0; i < number; i++) { | |
String nome = filename + i + extensao; | |
DataSource source = new FileDataSource(path); | |
messageBodyPart.setDataHandler(new DataHandler(source)); | |
messageBodyPart.setFileName(nome); | |
multipart.addBodyPart(messageBodyPart, i); | |
} | |
return multipart; | |
} | |
//Constantes | |
static final int TAMANHO_BUFFER = 4096; // 4kb | |
// método para compactar arquivo | |
public static void compactarParaZip(String arqSaida, String ...arqEntradas) throws IOException { | |
int cont; | |
byte[] dados = new byte[TAMANHO_BUFFER]; | |
BufferedInputStream origem = null; | |
FileInputStream streamDeEntrada = null; | |
FileOutputStream destino = null; | |
ZipOutputStream saida = null; | |
ZipEntry entry = null; | |
try { | |
destino = new FileOutputStream(new File(arqSaida)); | |
saida = new ZipOutputStream(new BufferedOutputStream(destino)); | |
for(final String arqEntrada : arqEntradas) { | |
File file = new File(arqEntrada); | |
streamDeEntrada = new FileInputStream(file); | |
origem = new BufferedInputStream(streamDeEntrada, TAMANHO_BUFFER); | |
entry = new ZipEntry(file.getName()); | |
saida.putNextEntry(entry); | |
while ((cont = origem.read(dados, 0, TAMANHO_BUFFER)) != -1) { | |
saida.write(dados, 0, cont); | |
} | |
origem.close(); | |
} | |
saida.close(); | |
} catch (IOException e) { | |
throw new IOException(e.getMessage()); | |
} | |
} | |
public static String[] listaArquivos(String diretorio) throws IOException { | |
File file = new File(diretorio); | |
File afile[] = file.listFiles(); | |
String[] lista = new String[afile.length]; | |
int i = 0; | |
for (int j = afile.length; i < j; i++) { | |
File arquivos = afile[i]; | |
System.out.println(arquivos.getName()); | |
lista[i] = diretorio+arquivos.getName(); | |
} | |
return lista; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment