集册 Java实例教程 通过消息发送电子邮件

通过消息发送电子邮件

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

492
通过消息发送电子邮件
//n o w j a v a . c o m - 时代Java 提供


import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.util.Properties;


public class Main{

    private static Session session = getSession();

    public static void sendEmail(String subject, String body) {

        try {


            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress("from@gmail.com"));

            message.setRecipients(Message.RecipientType.TO,

                    InternetAddress.parse("asdf@gmail.com"));

            message.setSubject(subject);

            message.setText(body);

            Transport.send(message);

            System.out.println("Done");/** 时代Jav
展开阅读全文