java发邮件

package com.server.jmail;



import java.util.*;

import java.io.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;



public class Email {



    public static void main(String[] args) {



        String to = "
88888888@qq.com";//接收人

        String from = "xxxxxx
@163.com";//发送人

        String host = "smtp.163.com";//邮件服务器

        String user = "
xxxxxx
@163.com";//发送账号,必须与发送人一致

        String pwd = "******";//授权码,不是密码



        Properties props = new Properties();

        props.put("mail.smtp.host", host);

        props.put("mail.debug", debug);

        Session session = Session.getInstance(props, null);

        session.setDebug(debug);



        try {

            MimeMessage msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(from));

            InternetAddress[] address = { new InternetAddress(to) };

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

            msg.setSubject("邮件主题,内容自定义");

            msg.setSentDate(new Date());

            msg.setText("
这是发送内容.还可以写很多很多的内容");



            // 发送邮件

            Transport transport;

            msg.saveChanges();

            transport = session.getTransport("smtp");

            transport.connect(host, user, pwd);

            transport.sendMessage(msg, msg.getAllRecipients());

        } catch (MessagingException e) {

            e.printStackTrace();

        }

    }

}

Author:

发表评论

电子邮件地址不会被公开。 必填项已用*标注