É uma framework para envio, recebimento e manipulação de e-mais, com
implementações para IMAP e SMTP.
Ambiente básico para utilização do JavaMail:
Ambiente básico para utilização do JavaMail:
JDK (Ambiente de desenvolviemento do JDK)
JavaMail (mail.jar), JAF (activation.jar)
Qmail (servidor de mails)
JavaMail (mail.jar), JAF (activation.jar)
Qmail (servidor de mails)
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
public class smtp_jm
{
public static void main(String args[]) throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com.br"); // definicao do host
props.put("mail.smtp.auth", "true"); // determina se tera autenticaçao
{
public static void main(String args[]) throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com.br"); // definicao do host
props.put("mail.smtp.auth", "true"); // determina se tera autenticaçao
Authenticator
auth = new SMTPAuthenticator(); // autenticador
Session s = Session.getInstance(props,auth);
s.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
s.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
MimeMessage message = new
MimeMessage(s);
InternetAddress
from = new InternetAddress("lilith_nefelin@yahoo.com.br");
message.setFrom(from);
message.setFrom(from);
InternetAddress
to = new InternetAddress("helloween_salvation@yahoo.com.br");
message.addRecipient(Message.RecipientType.TO, to);
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("E-Mail enviado a partir do JavaMail.");
message.setText("Este é o texto da mensagem enviada através do JavaMail!");
message.setText("Este é o texto da mensagem enviada através do JavaMail!");
Transport.send(message);
}
}
}
}
class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "lilith_nefelin";
String password = "manuka";
return new PasswordAuthentication(username, password);
}
}
Para a leitura de e-mails de um servidor que utilize o protocolo POP, é necessário que a mensagem do servidor seja copiada e aberta localmente.
Para um servidor IMAP, nos estaremos trabalhando com o servidor diretamente.
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "lilith_nefelin";
String password = "manuka";
return new PasswordAuthentication(username, password);
}
}
Para a leitura de e-mails de um servidor que utilize o protocolo POP, é necessário que a mensagem do servidor seja copiada e aberta localmente.
Para um servidor IMAP, nos estaremos trabalhando com o servidor diretamente.
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
public class pop_jm
{
public static void main(String args[]) throws Exception
{
String host= "pop.mail.yahoo.com.br";
String user= "helloween_salvation";
String pass= "manuka";
{
public static void main(String args[]) throws Exception
{
String host= "pop.mail.yahoo.com.br";
String user= "helloween_salvation";
String pass= "manuka";
Properties props = new Properties();
Authenticator auth = new SMTPAuthenticator(); // autenticador
Authenticator auth = new SMTPAuthenticator(); // autenticador
Session session = Session.getDefaultInstance(props,auth);
session.setDebug(false); // em caso de nao querer debugaçao suprima esta linha
session.setDebug(false); // em caso de nao querer debugaçao suprima esta linha
Store store =
session.getStore("pop3");
store.connect(host,user,pass);
store.connect(host,user,pass);
Folder folder =
store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
folder.open(Folder.READ_ONLY);
Message message[]= folder.getMessages();
System.out.println("\nTotal de Mensagens: "+message.length+"\n");
System.out.println("\nTotal de Mensagens: "+message.length+"\n");
// Display from (only first) and subject of
messages
for (int i=0, n=message.length; i<n; i++)
{
System.out.println("From: "+message[i].getFrom()[0]);
System.out.println("Subject: "+message[i].getSubject());
System.out.println("Mensagem: "+message[i].getContent());
}
for (int i=0, n=message.length; i<n; i++)
{
System.out.println("From: "+message[i].getFrom()[0]);
System.out.println("Subject: "+message[i].getSubject());
System.out.println("Mensagem: "+message[i].getContent());
}
folder.close(false);
store.close();
}
}
store.close();
}
}
class SMTPAuthenticator
extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "helloween_salvation";
String password = "manuka";
return new PasswordAuthentication(username, password);
}
}
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "helloween_salvation";
String password = "manuka";
return new PasswordAuthentication(username, password);
}
}
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
public class smtp_anexo
{
public static void main(String args[]) throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com.br"); // definicao do host
props.put("mail.smtp.auth", "true"); // determina se tera autenticaçao
{
public static void main(String args[]) throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com.br"); // definicao do host
props.put("mail.smtp.auth", "true"); // determina se tera autenticaçao
Authenticator
auth = new SMTPAuthenticator(); //
autenticador
Session s = Session.getInstance(props,auth);
s.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
s.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
MimeMessage message = new
MimeMessage(s);
InternetAddress
from = new InternetAddress("lilith_nefelin@yahoo.com.br");
message.setFrom(from);
message.setFrom(from);
InternetAddress
to = new InternetAddress("helloween_salvation@yahoo.com.br");
message.addRecipient(Message.RecipientType.TO, to);
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("E-Mail enviado a partir do JavaMail.");
message.setText("Este é o texto da mensagem enviada através do JavaMail!");
message.setText("Este é o texto da mensagem enviada através do JavaMail!");
// Neste ponto entra os Anexos
Multipart
multiparts = new MimeMultipart(); //
Objeto que compoem todas as partes da mensagem
BodyPart corpopart = new MimeBodyPart();
corpopart.setText("texto");
multiparts.addBodyPart(corpopart);
DataSource DS = new FileDataSource("c:/testes/thread.java");
corpopart.setDataHandler(new DataHandler(DS));
corpopart.setFileName("thread.java");
multiparts.addBodyPart(corpopart);
BodyPart corpopart = new MimeBodyPart();
corpopart.setText("texto");
multiparts.addBodyPart(corpopart);
DataSource DS = new FileDataSource("c:/testes/thread.java");
corpopart.setDataHandler(new DataHandler(DS));
corpopart.setFileName("thread.java");
multiparts.addBodyPart(corpopart);
message.setContent(multiparts);
Transport.send(message);
}
}
}
}
class SMTPAuthenticator
extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "lilith_nefelin";
String password = "manuka";
return new PasswordAuthentication(username, password);
}
}
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "lilith_nefelin";
String password = "manuka";
return new PasswordAuthentication(username, password);
}
}
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
public class listdir
{
public static void main(String args[]) throws Exception
{
String host= "pop.mail.yahoo.com.br";
String user= "helloween_salvation";
String pass= "manuka";
{
public static void main(String args[]) throws Exception
{
String host= "pop.mail.yahoo.com.br";
String user= "helloween_salvation";
String pass= "manuka";
Properties props = new Properties();
Authenticator auth = new SMTPAuthenticator(user,pass); // autenticador
Authenticator auth = new SMTPAuthenticator(user,pass); // autenticador
Session session = Session.getDefaultInstance(props,auth);
session.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
session.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
Store store =
session.getStore("pop3");
store.connect(host,user,pass);
store.connect(host,user,pass);
Folder root =
store.getDefaultFolder();
Folder folders[]= root.list();
for (int k=0;k < folders.length;k++)
{
Folder tmp = folders[k];
tmp.open(Folder.READ_ONLY);
System.out.println("Nome Completo: "+tmp.getFullName());
System.out.println("Nome: "+tmp.getName());
System.out.println("Qtde: "+tmp.getMessageCount());
System.out.println("Nao lidas: "+tmp.getUnreadMessageCount());
System.out.println("Novas mensagens: "+tmp.getNewMessageCount());
tmp.close(true);
}
store.close();
}
}
Folder folders[]= root.list();
for (int k=0;k < folders.length;k++)
{
Folder tmp = folders[k];
tmp.open(Folder.READ_ONLY);
System.out.println("Nome Completo: "+tmp.getFullName());
System.out.println("Nome: "+tmp.getName());
System.out.println("Qtde: "+tmp.getMessageCount());
System.out.println("Nao lidas: "+tmp.getUnreadMessageCount());
System.out.println("Novas mensagens: "+tmp.getNewMessageCount());
tmp.close(true);
}
store.close();
}
}
class SMTPAuthenticator
extends Authenticator
{
String username;
String password;
{
String username;
String password;
public SMTPAuthenticator(String user, String pass){
this.username= user;
this.password= pass;
}
this.username= user;
this.password= pass;
}
public
PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
{
return new PasswordAuthentication(username, password);
}
}
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.*;
public class makedir
{
public static void main(String args[]) throws Exception
{
String host= "pop.mail.yahoo.com.br";
String user= "helloween_salvation";
String pass= "manuka";
{
public static void main(String args[]) throws Exception
{
String host= "pop.mail.yahoo.com.br";
String user= "helloween_salvation";
String pass= "manuka";
Properties props = new Properties();
Authenticator auth = new SMTPAuthenticator(user,pass); // autenticador
Authenticator auth = new SMTPAuthenticator(user,pass); // autenticador
Session session = Session.getDefaultInstance(props,auth);
session.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
session.setDebug(true); // em caso de nao querer debugaçao suprima esta linha
Store store =
session.getStore("pop3");
store.connect(host,user,pass);
store.connect(host,user,pass);
Folder novo =
store.getFolder("teste");
if (!novo.exists())
novo.create(Folder.HOLDS_MESSAGES);
if (!novo.exists())
novo.create(Folder.HOLDS_MESSAGES);
store.close();
}
}
}
}
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.*;
import javax.mail.internet.*;
public class DeleteMessageExample {
public static void main (String args[]) throws Exception {
String host = "pop.ig.com.br";
String username = "torah_1999";
String password = "manoel";
public static void main (String args[]) throws Exception {
String host = "pop.ig.com.br";
String username = "torah_1999";
String password = "manoel";
Authenticator auth = new SMTPAuthenticator(); // autenticador
// Get session
Session session = Session.getInstance(System.getProperties(), auth);
session.setDebug(false);
// Get session
Session session = Session.getInstance(System.getProperties(), auth);
session.setDebug(false);
// Get the
store
Store store = session.getStore("pop3");
store.connect(host, username, password);
Store store = session.getStore("pop3");
store.connect(host, username, password);
// Get
folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
new InputStreamReader(System.in));
// Get
directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(message[i].getSubject());
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(message[i].getSubject());
System.out.println("Do you want to delete message? [YES to
delete]");
String line = reader.readLine();
// Mark as deleted if appropriate
if ("YES".equals(line)) {
message[i].setFlag(Flags.Flag.DELETED, true);
}
}
String line = reader.readLine();
// Mark as deleted if appropriate
if ("YES".equals(line)) {
message[i].setFlag(Flags.Flag.DELETED, true);
}
}
// Close
connection
folder.close(true);
store.close();
}
}
folder.close(true);
store.close();
}
}
class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "torah_1999";
String password = "manoel";
return new PasswordAuthentication(username, password);
}
}
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "torah_1999";
String password = "manoel";
return new PasswordAuthentication(username, password);
}
}
Recebe mensagens
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
public class RecebeMail {
public
static void main(String args[]) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new
RecebeMail().recebeMSG();
}
public void
recebeMSG() throws MessagingException {
Properties props = new Properties();
props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.socketFactory.fallback",
"false");
props.put("mail.store.protocol", "pop3");
Session
session = Session.getDefaultInstance(props,null);
Store
store = session.getStore("pop3");
//Estabelece
conexão
store.connect("pop.gmail.com",995, "USUARIO",
"SENHA");
//Obtem a pasta e a abre
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
//Obtem
mensagens
Message
msg[] = folder.getMessages();
for (int
i = 0; i < msg.length; i++){
try
{
System.out.println("MENSAGEM:" + i);
System.out.println("NOME :
" + ((InternetAddress) msg[i].getFrom()[0]).getPersonal() + "\n"
+
"FROM:
" + ((InternetAddress) msg[i].getFrom()[0]).getAddress());
System.out.println("DATA DE ENVIO: " + msg[i].getSentDate());
System.out.println("ASSUNTO "+msg[i].getSubject());
System.out.println("CONTEUDO " +msg[i].getContent());
System.out.println("FIM
DA MENSAGEM: " + i);
} catch (MessagingException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
folder.close(false);
store.close();
}
}

Nenhum comentário:
Postar um comentário