java发送邮件

package cn.itcast.javamail2;

import java.util.Properties;

import javax.mail.*;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Test2 {

	//测试成功
	public static void main(String[] args) {
		Properties props=new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		Session session=Session.getInstance(props);
		session.setDebug(true);
		Message msg=new MimeMessage(session);
		try {
			//msg.setText("你好");
			//发送的邮箱地址
			msg.setFrom(new InternetAddress("[email protected]"));
			msg.setSubject("hello");//邮件标题
           // msg.setSubject(MimeUtility.encodeText(title,"utf-8","hello"));//格式化邮件标题信息(处理中文乱码)
			msg.setContent("<span style='color:red'>hehe</span>","text/html;charset=gbk;");//邮件内容
			Transport transport=session.getTransport();
			//设置服务器以及账号和密码
			transport.connect("smtp.163.com",25,"zhao103804","******");
			//发送到的邮箱地址
			transport.sendMessage(msg,new Address[]{new InternetAddress("[email protected]")});
			transport.close();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,html)