首页 > 编程语言 > 详细

java发送邮件

时间:2014-06-26 23:25:40      阅读:358      评论:0      收藏:0      [点我收藏+]
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发送邮件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!