Created
September 26, 2011 19:34
-
-
Save antoniopassos/1243176 to your computer and use it in GitHub Desktop.
Componentes do JSampleCorreio
This file contains 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
package br.pro.antoniopassos.jsc; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.SimpleEmail; | |
public class Carteiro { | |
private String hostName = "smtp.gmail.com"; | |
private String usuario = "fulano.tal"; // Seu login do Gmail | |
private String senha = "S3nh@"; // Sua senha do Gmail | |
private String email = "fulano.tal@gmail.com"; // Seu e-mail do Gmail | |
private SimpleEmail simpleEmail; | |
public Carteiro() { | |
this.simpleEmail = new SimpleEmail(); | |
} | |
public void enviarMensagem(Mensagem mensagem) throws EmailException { | |
simpleEmail.setHostName(hostName); | |
simpleEmail.setAuthentication(usuario, senha); | |
simpleEmail.setSSL(true); | |
simpleEmail.setFrom(email); | |
simpleEmail.addTo(mensagem.getDestinatario()); | |
simpleEmail.setSubject(mensagem.getAssunto()); | |
simpleEmail.setMsg(mensagem.getMensagem()); | |
simpleEmail.send(); | |
} | |
} |
This file contains 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
package br.pro.antoniopassos.jsc; | |
public class Mensagem { | |
private String destinatario; | |
private String assunto; | |
private String mensagem; | |
public Mensagem() { | |
super(); | |
} | |
public Mensagem(String destinatario, String assunto, String mensagem) { | |
super(); | |
this.destinatario = destinatario; | |
this.assunto = assunto; | |
this.mensagem = mensagem; | |
} | |
public String getDestinatario() { | |
return destinatario; | |
} | |
public void setDestinatario(String destinatario) { | |
this.destinatario = destinatario; | |
} | |
public String getAssunto() { | |
return assunto; | |
} | |
public void setAssunto(String assunto) { | |
this.assunto = assunto; | |
} | |
public String getMensagem() { | |
return mensagem; | |
} | |
public void setMensagem(String mensagem) { | |
this.mensagem = mensagem; | |
} | |
} |
This file contains 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
package br.pro.antoniopassos.jsc; | |
import org.apache.commons.mail.EmailException; | |
public class Principal { | |
public static void main(String[] args) { | |
UserInterface ui = new UserInterface(); | |
Mensagem mensagem = ui.lerMensagem(); | |
Carteiro carteiro = new Carteiro(); | |
try { | |
carteiro.enviarMensagem(mensagem); | |
ui.exibirMsgSucesso(); | |
} catch (EmailException e) { | |
ui.exibirMsgErro(e.getMessage()); | |
} | |
} | |
} |
This file contains 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
package br.pro.antoniopassos.jsc; | |
import java.util.Scanner; | |
public class UserInterface { | |
private Scanner console; | |
public UserInterface (){ | |
this.console = new Scanner(System.in); | |
} | |
public Mensagem lerMensagem() { | |
System.out.println("De: fulano.tal@gmail.com" ); | |
System.out.print("Para: "); | |
String destinatario = console.nextLine(); | |
System.out.print("Assunto: "); | |
String assunto = console.nextLine(); | |
System.out.print("Mensagem: "); | |
String msg = console.nextLine(); | |
Mensagem mensagem = new Mensagem(); | |
mensagem.setDestinatario(destinatario); | |
mensagem.setAssunto(assunto); | |
mensagem.setMensagem(msg); | |
return mensagem; | |
} | |
public void exibirMsgSucesso(){ | |
System.out.println("\n==============================="); | |
System.out.println("Mensagem enviada com sucesso!!!"); | |
System.out.println("==============================="); | |
} | |
public void exibirMsgErro(String msg){ | |
System.out.println("\n*** Ops! Não foi possível enviar a mensagem ***"); | |
System.out.println("\nMotivo: \n" + msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment