javax.mail 发邮件方法一

import java.util.Properties;


import javax.mail.Message;

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,String userName,String password,String receive) throws Exception{

Session session = Session.getInstance(new Properties(), null);

MimeMessage m = new MimeMessage(session);

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

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

m.setSubject("hello3");

m.setText("nihao1");

m.saveChanges(); 

Transport transport = session.getTransport("smtp");

transport.connect(host, userName,password);

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

transport.close();

}

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

String host = "";

String userName = "";

String password = "";

String receive = "";

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

}

}


你可能感兴趣的:(javax.mail 发邮件方法一)