首页 > 代码库 > java发送邮件

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.MimeMessage;public class SendMail {         static Authenticator auth = new Authenticator() {        @Override        protected PasswordAuthentication getPasswordAuthentication() {            return new PasswordAuthentication("username@163.com", "pwd");//发件人的用户名和密码        }    };    public static void main(String[] args) {        Properties props = new Properties();        props.put("mail.smtp.host", "smtp.163.com");//        props.put("mail.smtp.auth", "true");        props.put("mail.from", "username@163.com");        Session session = Session.getInstance(props, auth);        try {            MimeMessage msg = new MimeMessage(session);            msg.setFrom();            msg.setRecipients(Message.RecipientType.TO, "收件人邮箱");            msg.setSubject("标题");            msg.setSentDate(new Date());            msg.setText("我这里是内容.........");            Transport.send(msg);        } catch (MessagingException mex) {            System.out.println("send failed, exception: " + mex);        }    }}



ps:需要导入mail.jar包
http://download.csdn.net/download/qq531783564/7548771