JavaMail

JavaMail
=======
邮件协议
SMTP(Simple Mail Transfer Portools)简单邮件传输协议:发邮件协议
POP3(Post Office Protocol Verson 3)邮局协议第三版:收邮件协议
IMAP(Internet Message Access Protocol)因特网消息访问协议 邮件收发协议

邮件服务器名称
smtp服务器端口号:25
pop3服务器端口号:110
eg:
163
smtp.163.com、pop3.163.com
126
smtp.126.com、pop3.126.com
=========
JavaMail
需要的Java包
mail.jar
activation.jar
核心类:
Session服务器连接对象
得到Session对象:Session.getInstance(Proporties,Authenticator)
设置属性mail.host,mail.stmp.path:Properits.setProperity()
得到Authenticator:
Authenticator auth = new Authenticator(){
    protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication("name","password");
    }
}
Session session=Session.getInstance(Proporties,Authenticator);
MimeMessage邮件对象
MimeMessage msg = new MimeMessage(session);
-setFrom(),设置发件人、设置收件人,设置正文
setFrom(new InternetAddress("发件人")):设置发件人
setRecipients(RecipientType.TO/CC/BCC,收件人):设置收件人,抄送,暗送
setSubject(String);设置主题
setContent(String,Sting);设置主题
Transport
-send(MimeMessage)

不要忘记先去邮箱打开SMTP服务

发送纯文本:

	public void fun() throws Exception{
		//得到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("账号","密码");
			}
		};
		
		Session session =Session.getDefaultInstance(props,auth);
		
		//创建MimeMessage
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("xxx"));//设置发件人
		msg.setRecipients(RecipientType.TO, "收件人邮箱");
		
		msg.setSubject("Test Email");//设置主题
		msg.setContent("This is a Test Email","text/html;charset=utf-8");//设置内容
	
		//发送
		Transport.send(msg);
	}

发送文件:

public void fun() throws Exception{
		//得到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("账号","密码");
			}
		};
		
		Session session =Session.getDefaultInstance(props,auth);
		
		//创建MimeMessage
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("Xxx"));//设置发件人
		msg.setRecipients(RecipientType.TO, "收件人邮箱");
		
		msg.setSubject("Test Email");//设置主题
		//设置多部件的部件内容
		MimeMultipart list = new MimeMultipart();//创建多部件内容
		MimeBodyPart part1 = new MimeBodyPart();//创建单个部件
		part1.setContent("This is a Test Message", "text/html;charset=utf-8");//设置内容
		list.addBodyPart(part1);//加入多部件中
		
		MimeBodyPart part2 = new MimeBodyPart();
		part2.attachFile(new File("E:/Picture/12809803194626.jpg"));
		part2.setFileName(MimeUtility.encodeText("picture.jpg"));
		list.addBodyPart(part2);
		
		//设置到邮件内容
		msg.setContent(list);
		//发送
		Transport.send(msg);
	}
}



   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值