码迷,mamicode.com
首页 > 编程语言 > 详细

java发送邮件

时间:2014-06-26 23:26:04      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

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

 

java发送邮件,布布扣,bubuko.com

java发送邮件

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/xlh91118/p/3807674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!