首页 > 编程语言 > 详细

javax.mail 发邮件方法二

时间:2015-04-09 17:53:58      阅读:228      评论:0      收藏:0      [点我收藏+]

import java.util.Properties;


import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;


public class TestEmail {

/**

* @param  host 邮箱发件服务器

* @param  userName 发送人用户名,一般是邮箱名

* @param  password 邮箱密码

* @param  receive 收件人邮箱地址

* @throws  Exception

*/

public static void sendMail(String host,final String userName,final String password,String receive) throws Exception{

Properties props = new Properties();

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

props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName,password);

}});

MimeMessage m = new MimeMessage(session);

m.setFrom(new InternetAddress(userName,"huhao"));

m.setRecipients(Message.RecipientType.TO, receive);

m.setSubject("hello3");

m.setText("nihao1");

Transport.send(m);

}

public static void main(String[] args) throws Exception{

String host = "";//例如qq邮箱smtp.qq.com

String userName = "";

String password = "";

String receive = "";

TestEmail.sendMail(host,userName,password,receive);

}

}


javax.mail 发邮件方法二

原文:http://my.oschina.net/hu382337381/blog/398190

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