Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.9 kB
3
Indexable
Never
package com.veliz.mail;

import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.Transport;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 *
 * @author erve
 */
public class Mail {

    public static void main(String[] args) throws UnsupportedEncodingException {
        final String username = "evelize1400@alumno.ipn.mx";
        final String toUser = "erve@live.com.mx";
        final String password = "";

        final String host = "smtp.office365.com";
        
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");
        
        Session session = Session.getInstance(properties, new jakarta.mail.Authenticator(){
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        
        try{
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username, "NoReply"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toUser));
            message.setSubject("Certificados");
            message.setText("Estoy enviando con la nueva dependencia.");
            Transport.send(message);
            
            System.out.println("Enviado");
            
        }catch(MessagingException e){
            throw new RuntimeException(e);
        }
        
    }
}