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("eric@163.com","123456");
			}
		};
		Session session = Session.getDefaultInstance(props , authenticator );
		
		MimeMessage mail = new MimeMessage(session);
		
		
		mail.setFrom(new InternetAddress("ericxu12345@sina.com"));
		mail.setRecipient(RecipientType.TO, new InternetAddress("jackyxu@163.com"));
	
		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("eric@163.com", "123456");
			}
		};
		
		
		//创建连接并登陆服务器,
		//参数一为properties文件表示登陆的主机地址和登陆配置,参数二为指定用户名和密码(需要验证登录的(加密的))
		Session session = Session.getDefaultInstance(props, authenticator);
		
		session.setDebug(true);
		
		//创建邮件
		MimeMessage mail = new MimeMessage(session);
		
				
				//2.1 指定发件人(和登录用户一致)
				mail.setFrom(new InternetAddress("ericxu12345@sina.com"));
				
				//2.2 指定收件人
				//参数一:收件类型   TO:收件人   CC:抄送人  BCC:密送人
				//例如: A -> B(TO) -> C(CC) -> D(BCC)
				
				mail.setRecipient(RecipientType.TO, new InternetAddress("jackyxu12345@sina.com"));
				
				//2.3 主题
				mail.setSubject("这是一封javamail发送的测试邮件");
				
				//2.4 内容
				mail.setContent("测试邮件内容,七夕快乐!", "text/plain;charset=utf-8");
				
				//3)发送邮件
				Transport.send(mail);
		
	}
}


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值