Javamail发送邮件

1、创建session
  (1)设置两个参数
    >Properties(1、setProperty("mail.host","smto.163.com"),2、   setProperty("mail.smtp.auth","true")// 是否需要认证)
    >Authenticator(利用其内部类,覆盖getPasswordAuthentication()方法,并返回其构造方法,参数有登录名(邮箱去掉163.com)和登录密码)
2、创建MimeMessage,设置发送人,收件人等以及邮件主题和内容

3、调用Transport.send(msg)来发送邮件


注意:163邮箱必须要设置客户端授权密码,才能开启SMTP服务器,这样才能用javaMail来实现发邮件(而且登录密码必须为授权密码)


package cn.action.mail.demo;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Test;

public class Demo1 {
	public static void main(String[] args) throws AddressException,
			MessagingException {

		/*
		 * 1、得到session(创建连接)
		 */

		// 设置session的参数properties
		Properties prop = new Properties();
		prop.setProperty("mail.host", "smtp.163.com");
		prop.setProperty("mail.smtp.auth", "true");

		// 设置session的参数Authenticator
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				// TODO Auto-generated method stub
				return new PasswordAuthentication("zexin511123",
						"密码");
			}
		};

		// 参数设置文本,new一个session
		Session session = Session.getInstance(prop, auth);

		/*
		 * 2、创建MimeMessage
		 */

		MimeMessage mes = new MimeMessage(session);
		mes.setFrom(new InternetAddress("[email protected]"));
		mes.setRecipients(RecipientType.TO, "[email protected]");
		mes.setRecipients(RecipientType.CC, "[email protected]");// 设置抄送
		mes.setRecipients(RecipientType.BCC, "[email protected]");// 设置暗送
		mes.setSubject("这是一封垃圾邮件");
		mes.setContent("Hello World", "text/html;charset=utf-8");

		Transport.send(mes);

	}

	/*
	 * 带有附件的发送
	 */
	@Test
	public void fun2() throws AddressException, MessagingException, IOException {
		//1、得到session
		Properties props = new Properties();
		props.setProperty("mail.host", "smtp.163.com");
		props.setProperty("mail.smtp.auth", "true");
		
		Authenticator auth = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("zexin511123", "密码");
			}
		};
		
		Session session = Session.getInstance(props, auth);
		
		//2、创建MimeMessage
		MimeMessage msg = new MimeMessage(session);
		/*
		 * 设置发件人和收件人以及邮件主题
		 */
		msg.setFrom(new InternetAddress("[email protected]"));
		msg.setRecipients(RecipientType.TO, "[email protected]");
		msg.setSubject("这是一封带有附件的测试邮件,哈哈");
		
		/*
		 * 多部件发送
		 * 1、创建MimeMultipart,其实就是一个集合,来存放邮件的各个主体部件
		 * 2、创建MimeBodyPart,邮件的主体部件
		 * 3、把邮件的主体部件存到集合中
		 * 4、把集合设置为MimeMessage的内容
		 */
		MimeMultipart multipart = new MimeMultipart();
		MimeBodyPart part1 = new MimeBodyPart();
		part1.setContent("这是一封带有附件的测试邮件", "text/htm;charset=utf-8");
		multipart.addBodyPart(part1);
		
		MimeBodyPart part2 = new MimeBodyPart();
		part2.attachFile(new File("c:/张学友 - 头发乱了.mp3"));
		part2.setFileName(MimeUtility.encodeText("头发乱了--张学友"));
		multipart.addBodyPart(part2);
		
		msg.setContent(multipart);
		
		//3、发送邮件
		Transport.send(msg);
	}
}







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