javaMail入门实例

package August;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 必须导入smtp.jar包,如果导入包后发生异常(找不到原因)
 * 尝试将MyEclipse中自带的mail相关的包删除
 * @author snaillocke
 *
 */
public class JavaMail {
	public static void main(String[] args) throws MessagingException {
		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 message =  new MimeMessage(session);
		message.setText("发件内容");
		message.setFrom(new InternetAddress("发件邮箱地址"));
		Transport transport = session.getTransport();
		transport.connect("smtp.xx.com",25,"发件邮箱用户名","发件邮箱密码");
		transport.sendMessage(message, new Address[]{new InternetAddress("收件邮箱地址")});
		transport.close();
	}

}

你可能感兴趣的:(MyEclipse)