javaee之javamail邮件练习


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

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;

public class demo2 {

	public static void main(String[] args) throws Exception {
		
		//创建一个登陆连接服务器的文件
		Properties props = new Properties();
		props.setProperty("mail.host", "smtp.sina.com");
		props.setProperty("mail.smtp.auth", "true");
		
		//登陆的用户名和密码
		Authenticator authenticator = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				// TODO Auto-generated method stub
				return new PasswordAuthentication("[email protected]","123456");
			}
		};
		Session session = Session.getDefaultInstance(props , authenticator );
		
		MimeMessage mail = new MimeMessage(session);
		
		
		mail.setFrom(new InternetAddress("[email protected]"));
		mail.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
	
		mail.setSubject("这是一个主题");
		
		MimeBodyPart body1 = new MimeBodyPart();
		body1.attachFile(new File("c:/images.jpg"));
		MimeBodyPart body2 = new MimeBodyPart();
		body2.attachFile(new File("c:/images.jpg"));
		MimeBodyPart body3 = new MimeBodyPart();
		body3.setContent("youyisi", "text/html;charset=utf-8");
		
		MimeMultipart mmp = new MimeMultipart();
		mmp.addBodyPart(body3, 0);
		mmp.addBodyPart(body2, 1);
		mmp.addBodyPart(body1, 2);
		
		mail.setContent(mmp);
		
		Transport.send(mail);
		
	}
}

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class demo1 {

	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.setProperty("mail.host", "smtp.sina.com"); //登陆的邮箱的主机
		props.setProperty("mail.smtp.auth", "true"); //是否验证加密
		
		//创建一个Authenticator的子类对象,对用户名和密码进行加密
		Authenticator authenticator = new Authenticator() {
			
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("[email protected]", "123456");
			}
		};
		
		
		//创建连接并登陆服务器,
		//参数一为properties文件表示登陆的主机地址和登陆配置,参数二为指定用户名和密码(需要验证登录的(加密的))
		Session session = Session.getDefaultInstance(props, authenticator);
		
		session.setDebug(true);
		
		//创建邮件
		MimeMessage mail = new MimeMessage(session);
		
				
				//2.1 指定发件人(和登录用户一致)
				mail.setFrom(new InternetAddress("[email protected]"));
				
				//2.2 指定收件人
				//参数一:收件类型   TO:收件人   CC:抄送人  BCC:密送人
				//例如: A -> B(TO) -> C(CC) -> D(BCC)
				
				mail.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
				
				//2.3 主题
				mail.setSubject("这是一封javamail发送的测试邮件");
				
				//2.4 内容
				mail.setContent("测试邮件内容,七夕快乐!", "text/plain;charset=utf-8");
				
				//3)发送邮件
				Transport.send(mail);
		
	}
}


JavaMail邮件发送功能
	通过使用SMTP协议进行邮件的发送
	
	1)创建并登陆服务器,并对用户名和密码进行登陆加密
	2)创建一份邮件,包括发件人、收件人、主题、内容
	3)发送邮件

其实mail邮件这个功能也是在特定的需求中才会被使用到

你可能感兴趣的:(javaee)