集册 Java实例教程 通过身份验证器发送电子邮件

通过身份验证器发送电子邮件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

529
通过身份验证器发送电子邮件


import EJB.EjbUsuario;//n o w j a v a . c o m - 时代Java 提供

import java.util.Date;

import java.util.Properties;

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;


public class Main{

    public static void sendEmail(String host, String port,

            final String userName, final String password, String toAddress,

            String subject, String message) throws AddressException,

            MessagingException {//时代Java公众号


        // sets SMTP server properties


        Properties properties = new Properties();


        properties.put("mail.smtp.host", "smtp.gmail.com");


        properties.put("mail.smtp.port", "587");


        properties.put("mail.smtp.auth", "true");

        properties.put("mail.smtp.starttls.enable", "true");


        // creates a new session with an authenticator

        Authenticator auth = new Authenticator() {


            @Override

            public PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(

                        "pruebatesisskynet@gmail.com", "skynet2000");

            }

        };


        Session session = Session.getInstance(properties, auth);

        // creates a new e-mail message

        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(toAddress));

        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };

        msg.setRecipients(Message.RecipientType.TO, toAddresses);

        msg.setSubject("Recuperar contrase?a Bosque de Hiria");

        msg.setSentDate(new Date());

        Encriptado encriptado = 
展开阅读全文