The Sample JavaMail_Send Demo

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Demo2 {


	public static void main(String[] args) throws Exception{
		
	Properties prop = new Properties();
	prop.setProperty("mail.smtp.auth", "true");//定义登陆方式
        prop.setProperty("mail.transport.protocol","smtp");//定义协议方式		   

	Session session = Session.getDefaultInstance(prop);

	Message msg = new MimeMessage(session);//1、发送信息
       //发送消息的一些协议等等要以Session方式设置

	session.setDebug(true);//显示debug
	msg.setSubject("测试");//主题
	msg.setText("你好");//context
	msg.setFrom(new InternetAddress("[email protected]"));//发件人
		
	Transport transport = session.getTransport();//2、连接
	transport.connect("smtp.163.com",25,"[email protected]","zhanzhan5606335");

transport.sendMessage(msg, new Address[{newInternetAddress("[email protected]")});

          transport.close();	
	}

}

你可能感兴趣的:(qq)