Open vazvdr opened 1 year ago
import javax.swing.; import java.sql.; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.JFrame; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.joda.time.DateTime;
public class JavaMailApp {
private Session criarSessionMail() { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", 465); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", true); props.put("mail.smtp.port", 465); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication (sistema.slxadm@gmail.com, 8520147); } }); session.setDebug(true); return session;
}
public static void main(String[] args){} private void enviarEmail() throws AddressException, MessagingException { String msg = jTextAreaMensagem.getText(); String assunto = jTextFieldAssunto.getText(); String email = jTextFieldEmailDestino.getText(); String remetente = "remetente@gmail.com"; System.out.println("Enviando email DE: " + remetente + " PARA: " + email); System.out.println("Assunto: " + assunto); Message message = new MimeMessage(criarSessionMail()); message.setFrom(new InternetAddress(remetente)); // Remetente Address[] toUser = InternetAddress // Destinatário(s) .parse(email.trim().toLowerCase()); message.setRecipients(Message.RecipientType.TO, toUser); message.setSubject(assunto);// Assunto message.setContent(msg, "text/html"); /** Método para enviar a mensagem criada */ Transport.send(message); System.out.println("Email enviado com sucesso !"); System.out.println("__________________________________________________");
Opa irmão vale lembrar que tem que configurar a conta do google para utiliza-la como host para enviar os emails
import javax.swing.; import java.sql.; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.JFrame; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.joda.time.DateTime;
public class JavaMailApp {
}
}