Commit c8bc507117948c3226679f24749cb6f0d03c4847
1 parent
45c6176c
Exists in
master
nova classe java para implementar contato
Showing
1 changed file
with
118 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,118 @@ |
1 | +package br.com.model.entity; | |
2 | + | |
3 | +import java.util.Properties; | |
4 | + | |
5 | +import javax.mail.Authenticator; | |
6 | +import javax.mail.Message; | |
7 | +import javax.mail.PasswordAuthentication; | |
8 | +import javax.mail.Session; | |
9 | +import javax.mail.Transport; | |
10 | +import javax.mail.internet.InternetAddress; | |
11 | +import javax.mail.internet.MimeMessage; | |
12 | + | |
13 | +public class EnvioEmailSemAutenticacao { | |
14 | + private String mailSMTPServer; | |
15 | + private String mailSMTPServerPort; | |
16 | + | |
17 | + /* | |
18 | + * caso queira mudar o servidor e a porta, so enviar para o contrutor os | |
19 | + * valor como string | |
20 | + */ | |
21 | + | |
22 | + public EnvioEmailSemAutenticacao() { | |
23 | + | |
24 | + } | |
25 | + | |
26 | + public EnvioEmailSemAutenticacao(String mailSMTPServer, | |
27 | + String mailSMTPServerPort) { // Para outro Servidor | |
28 | + this.mailSMTPServer = mailSMTPServer; | |
29 | + this.mailSMTPServerPort = mailSMTPServerPort; | |
30 | + } | |
31 | + | |
32 | + public String sendMail(String from, String to, String subject, | |
33 | + String message, String comAutenticacao, String authuser, | |
34 | + String authpass) { | |
35 | + Properties props = new Properties(); | |
36 | + String retorno = ""; | |
37 | + | |
38 | + props.put("mail.transport.protocol", "smtp"); // define protocolo de | |
39 | + // envio como SMTP | |
40 | + props.put("mail.smtp.starttls.enable", "true"); | |
41 | + props.put("mail.smtp.host", mailSMTPServer); // server SMTP do GMAIL | |
42 | + props.put("mail.smtp.auth", comAutenticacao); // ativa autenticacao | |
43 | + props.put("mail.smtp.user", from); // usuario ou seja, a conta que esta | |
44 | + // enviando o email (tem que ser do | |
45 | + // GMAIL) | |
46 | + props.put("mail.debug", "false"); | |
47 | + props.put("mail.smtp.port", mailSMTPServerPort); // porta | |
48 | + | |
49 | + // Cria um autenticador que sera usado a seguir | |
50 | + SimpleAuth auth = null; | |
51 | + auth = new SimpleAuth(authuser, authpass); | |
52 | + // Session - objeto que ira realizar a conex�o com o servidor | |
53 | + /* | |
54 | + * Como h� necessidade de autentica��o � criada uma autenticacao que � | |
55 | + * responsavel por solicitar e retornar o usu�rio e senha para | |
56 | + * autentica��o | |
57 | + */ | |
58 | + Session session = Session.getDefaultInstance(props, auth); | |
59 | + session.setDebug(true); // Habilita o LOG das a��es executadas durante o | |
60 | + // envio do email | |
61 | + // Objeto que cont�m a mensagem | |
62 | + Message msg = new MimeMessage(session); | |
63 | + try { | |
64 | + // Setando o destinat�rio | |
65 | + msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); | |
66 | + // Setando a origem do email | |
67 | + msg.setFrom(new InternetAddress(from)); | |
68 | + // Setando o assunto | |
69 | + msg.setSubject(subject); | |
70 | + // Setando o conte�do/corpo do email | |
71 | + msg.setContent(message, "text/html; charset=utf-8"); | |
72 | + | |
73 | + } catch (Exception e) { | |
74 | + retorno = e.getMessage(); | |
75 | + System.out.println(">> Erro: Completar Mensagem"); | |
76 | + e.printStackTrace(); | |
77 | + return retorno; | |
78 | + } | |
79 | + // Objeto encarregado de enviar os dados para o email | |
80 | + Transport tr; | |
81 | + try { | |
82 | + tr = session.getTransport("smtp"); // define smtp para transporte | |
83 | + /* | |
84 | + * 1 - define o servidor smtp 2 - seu nome de usuario do gmail 3 - | |
85 | + * sua senha do gmail | |
86 | + */ | |
87 | + tr.connect(mailSMTPServer, authuser, authpass); | |
88 | + msg.saveChanges(); // don't forget this | |
89 | + // envio da mensagem | |
90 | + tr.sendMessage(msg, msg.getAllRecipients()); | |
91 | + | |
92 | + tr.close(); | |
93 | + | |
94 | + } catch (Exception e) { | |
95 | + // TODO Auto-generated catch block | |
96 | + System.out.println(">> Erro: Envio Mensagem"); | |
97 | + retorno = e.getMessage(); | |
98 | + } | |
99 | + return retorno; | |
100 | + } | |
101 | + | |
102 | +} | |
103 | + | |
104 | +// clase que retorna uma autenticacao para ser enviada e verificada pelo | |
105 | +// servidor smtp | |
106 | +class SimpleAuth extends Authenticator { | |
107 | + public String username = null; | |
108 | + public String password = null; | |
109 | + | |
110 | + public SimpleAuth(String user, String pwd) { | |
111 | + username = user; | |
112 | + password = pwd; | |
113 | + } | |
114 | + | |
115 | + protected PasswordAuthentication getPasswordAuthentication() { | |
116 | + return new PasswordAuthentication(username, password); | |
117 | + } | |
118 | +} | |
0 | 119 | \ No newline at end of file | ... | ... |