Guest User

EMAILCLASS_java

a guest
Nov 12th, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 KB | None | 0 0
  1. package me.nao.email;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.Properties;
  5.  
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.Multipart;
  9. import javax.mail.PasswordAuthentication;
  10. import javax.mail.Session;
  11. import javax.mail.Transport;
  12. import javax.mail.Flags.Flag;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeBodyPart;
  15. import javax.mail.internet.MimeMessage;
  16. import javax.mail.internet.MimeMultipart;
  17.  
  18. public class EmailProcess {
  19.  
  20.    
  21.    
  22.    
  23.    
  24.    
  25.  
  26.    
  27.    
  28.    
  29.     public void sendEmail() {
  30.        
  31.          
  32.            // email ID of Recipient. String recipient = "[email protected]";
  33.           String recipient = "[email protected]";
  34.      
  35.        
  36.      
  37.          
  38.          
  39.           Properties properties = new Properties();
  40.           properties.put("mail.smtp.auth", "true");
  41.           properties.put("mail.smtp.starttls.enable", "true");
  42.         // properties.put("mail.smtp.host", "smtp.gmail.com");
  43.           properties.put("mail.smtp.host", "smtp.office365.com");
  44.           properties.put("mail.smtp.port", 587);
  45.           //
  46.         //   properties.put("mail.smtp.port", 587);
  47.        
  48.      
  49.           System.out.println("paso 1 ");
  50.          
  51.           Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
  52.              
  53.              
  54.              
  55.               protected PasswordAuthentication getPasswordAuthentication() {
  56.                
  57.                   return new PasswordAuthentication("[email protected]","password");
  58.                  
  59.               }
  60.              
  61.              
  62.              
  63.              
  64.              
  65.           });
  66.          
  67.          
  68.           System.out.println("paso 2");
  69.           try{
  70.              
  71.               System.out.println("paso 3 ");
  72.              
  73.               //PERMITE AÑADIR MAS SECCIONES DE TEXTO O DOCUMENTOS OJO
  74.               MimeBodyPart adjunto2 = new MimeBodyPart();
  75.               String texto = "holaaa este es un texto de prueba";
  76.               adjunto2.setContent("<h1 style=color:green>Titulo de Correo 3</h1>"
  77.                         + "<h2>Hola xdxdxd</h2>"
  78.                         + "<h3>Texto de ejemplo Hola mundo</h3>"
  79.                         + "<b>Negrita moment</b>"
  80.                         + "<a href=\"https://www.youtube.com/\">  Ir al link</a>"
  81.                    
  82.                        
  83.                         + "<p>"+texto+"</p>","text/html");
  84.              
  85.               MimeBodyPart adjunto = new MimeBodyPart();
  86.               //añade un file con ruta
  87.              // adjunto.attachFile("a");
  88.               adjunto.setFileName("test.txt");
  89.               adjunto.setText("holaaa ");
  90.               adjunto.setDescription("esto es otro test 32");
  91.              
  92.              
  93.               Multipart multipart = new MimeMultipart();
  94.  
  95.                  // Set text message part
  96.                  multipart.addBodyPart(adjunto);
  97.                  multipart.addBodyPart(adjunto2);
  98.              
  99.                  // MimeMessage object.
  100.                  MimeMessage message = new MimeMessage(session);
  101.              
  102.          
  103.                  // Set From Field: adding senders email to from field.
  104.                  
  105.                  try {
  106.                      //PARECE QUE DA IGUAL SI ES NULL (SOLIA IR EL SENDER)
  107.                      message.setFrom(new InternetAddress(null, "HOLA"));
  108.                  }catch(UnsupportedEncodingException e) {
  109.                      
  110.                  }
  111.                
  112.              
  113.          
  114.                  // Set To Field: adding recipient's email to from field.
  115.                  message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
  116.              
  117.                  // Set Subject: subject of the email
  118.                
  119.                 message.setSubject("PRUEBAAA ");
  120.                 message.setContentID("minecraft");
  121.                 message.addHeaderLine("lol");
  122.                
  123.                 message.setFlag(Flag.SEEN, true);
  124.                 message.setFlag(Flag.USER, true);
  125.                
  126.                 //message.setDisposition("holaaaa esto es de dispo");
  127.          
  128.                 /*
  129.                  // Create the message part
  130.                  
  131.  
  132.                  // Fill the message
  133.                   messageBodyPart.setText("This is message body");
  134.                  
  135.                  // Create a multipar message
  136.                  Multipart multipart = new MimeMultipart();
  137.  
  138.                  // Set text message part
  139.                  multipart.addBodyPart(messageBodyPart);
  140.  
  141.                  // Part two is attachment
  142.                  messageBodyPart.setText("This is message body");
  143.                  messageBodyPart = new MimeBodyPart();
  144.                  String filename = "file.txt";
  145.                  DataSource source = new FileDataSource(filename);
  146.                  messageBodyPart.setDataHandler(new DataHandler(source));
  147.                  messageBodyPart.setFileName(filename);
  148.                  multipart.addBodyPart(messageBodyPart);
  149.  
  150.                  // Send the complete message parts
  151.                
  152.                  
  153.                  message.setContent(multipart);
  154.                  
  155.                  */
  156.            
  157.                  
  158.              
  159.                 // String link = "https://www.youtube.com/";
  160.                  // set body of the email.
  161.                
  162.                 message.setContent(multipart);
  163.              
  164.                
  165.              
  166.              
  167.             //   "  <input type=\"file.txt\" name=\"adjunto\" accept=\".pdf,.jpg,.png\" multiple />\r\n" +
  168.                
  169.          
  170.                  // Send email.
  171.  
  172.                  Transport.send(message);
  173.              
  174.                  
  175.                  System.out.println("Mail successfully sent");
  176.               }
  177.               catch (MessagingException mex){
  178.                  
  179.                  mex.printStackTrace();
  180.               }
  181.          
  182.          
  183.        
  184.        
  185.     }
  186.    
  187.    
  188.    
  189. }
  190.  
Add Comment
Please, Sign In to add comment