Guest User

Untitled

a guest
Nov 21st, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.io.OutputStreamWriter;
  10. import java.io.PrintWriter;
  11. import java.net.InetAddress;
  12. import java.net.Socket;
  13. import java.net.UnknownHostException;
  14. import java.sql.Date;
  15. import java.text.DateFormat;
  16. import java.util.Base64;
  17. import java.util.Locale;
  18.  
  19. import javax.net.ssl.SSLSocket;
  20. import javax.net.ssl.SSLSocketFactory;
  21. /**
  22. *
  23. * @author Joubert Joe Vieira Lellis
  24. *
  25. */
  26. public class ProtocoloEmail {
  27.  
  28. public String log;
  29.  
  30.  
  31. /**
  32. *
  33. * @param emailBody - Corpo do email - Body of the Email
  34. * @param emailSubject - Assunto do EMail - Subject of the Email
  35. * @param emailSender - Remetente do email - Email Sender
  36. * @param emailRecipient - Destinat�rio do Email - Email Recipiente
  37. * @param emailHost - HOST (Gmail, Hotmail, Yahoo)
  38. * @throws IOException
  39. * @throws UnknownHostException
  40. */
  41. public void enviar( String emailBody, String emailSubject, String emailSender,String emailRecipient,String emailHost, String password) throws IOException, UnknownHostException{
  42.  
  43. SMTP mail = new SMTP(emailHost);
  44. // Protocolo - Protocol (Simple Mail Transpor Protocol)
  45.  
  46. if (mail != null) {
  47. if (mail.send(emailSubject , emailBody, emailSender, emailRecipient, password)) {
  48. log = ("Email Enviado n Mail Sent");
  49. } else {
  50. log = ("Falha ao conectar com servidor SMTP n Connect to SMTP server failed!");
  51. }
  52. }
  53. System.out.println("Pronto. n Done.");
  54. }
  55.  
  56. /**
  57. *
  58. * @author Joubert Joe Vieira Lellis
  59. *
  60. */
  61. static class SMTP {
  62. private final static int SMTP_PORT = 587; // Porta padr�o SMTP - Default Port SMTP
  63. InetAddress mailHost;
  64. InetAddress localhost;
  65. BufferedReader in;
  66. PrintWriter out;
  67.  
  68. /**
  69. *
  70. * @param host Host a ser usado - Host to be used. Ex: (gmail, hotmail, yahoo, etc...)
  71. * @throws UnknownHostException Tratamento de exce��o de Host - Host Exception
  72. */
  73. public SMTP(String host) throws UnknownHostException {
  74. mailHost = InetAddress.getByName(host);
  75. localhost = InetAddress.getLocalHost();
  76. System.out.println("mailhost = " + mailHost);
  77. System.out.println("localhost= " + localhost);
  78. System.out.println("Contrutor SMTP pronto. /n SMTP constructor donen");
  79. }
  80.  
  81. /**
  82. *
  83. * @param msgEmail Mensagem contendo Assunto e corpo de email - Mensage includind subject and body of the email.
  84. * @param from Quem vai enviar o email - Who gonna send that Email.
  85. * @param to Quem est� recebendo o email - Who gonna recieve that email.
  86. * @return Booleana para saber se tudo ocorreu bem - Boolean to check if everythings is right.
  87. * @throws IOException Poss�veis erros - Possible errors.
  88. */
  89. public boolean send(String msgAssunto, String msgEmail, String from, String to, String pass) throws IOException {
  90. // SSLSocket smtpSocket;
  91. Socket smtpSocket;
  92. InputStream inn;
  93. OutputStream outt;
  94.  
  95. // Criptografia
  96. String user = Base64.getEncoder().encodeToString(from.getBytes("utf-8"));
  97. pass = Base64.getEncoder().encodeToString(pass.getBytes("utf-8"));
  98.  
  99. // smtpSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(mailHost, SMTP_PORT);
  100. smtpSocket = new Socket(mailHost, SMTP_PORT);
  101.  
  102. if (smtpSocket == null) {
  103. return false;
  104. }
  105. inn = smtpSocket.getInputStream();
  106. outt = smtpSocket.getOutputStream();
  107. in = new BufferedReader(new InputStreamReader(inn));
  108. out = new PrintWriter(new OutputStreamWriter(outt), true);
  109. if (inn == null || outt == null) {
  110. System.out.println("Failed to open streams to socket.");
  111. return false;
  112. }
  113. String initialID = in.readLine();
  114. System.out.println(initialID);
  115. System.out.println("HELO ailton.eng.br");
  116. out.println("HELO ailton.eng.br");
  117. out.flush();
  118. String welcome = in.readLine();
  119. System.out.println(welcome);
  120. System.out.println("AUTH LOGIN");
  121. out.println("AUTH LOGIN");
  122. out.flush();
  123. out.println(user);
  124. out.flush();
  125. System.out.println(user);
  126. out.println(pass);
  127. out.flush();
  128. System.out.println(pass);
  129.  
  130. System.out.println("MAIL From:<" + from + ">");
  131. out.println("MAIL FROM:<" + from + ">");
  132. out.flush();
  133.  
  134. String senderOK = in.readLine();
  135. System.out.println(senderOK);
  136.  
  137. System.out.println("RCPT TO:<" + to + ">");
  138. out.println("RCPT TO:<" + to + ">");
  139. out.flush();
  140. String recipientOK = in.readLine();
  141. System.out.println(recipientOK);
  142.  
  143. System.out.println("DATA");
  144. out.println("DATA");
  145. out.flush();
  146.  
  147. out.println("Subject: " +msgAssunto);
  148. out.flush();
  149. System.out.println(msgAssunto);
  150.  
  151. out.println(msgEmail + "n.nQUITn");
  152. out.flush();
  153. System.out.println(msgEmail);
  154.  
  155. System.out.println(".");
  156.  
  157. out.flush();
  158. System.out.println("QUIT");
  159. // out.println("QUIT");
  160. out.flush();
  161. String acceptedOK = in.readLine();
  162. String resposta;
  163.  
  164.  
  165.  
  166. System.out.println(acceptedOK);
  167. /*
  168. do{
  169. resposta = in.readLine();
  170.  
  171. System.out.println(resposta);
  172. }while(resposta != null);
  173. */
  174.  
  175. return true;
  176. }
  177.  
  178. }
  179.  
  180. }
Add Comment
Please, Sign In to add comment