javaMail简单使用

 

JavaMail基本操作

1.         创建properties对象

2.         properties对象添加属性

l         添加发送邮件的邮件服务器属性

l         添加帐号密码校验属性

3.         用properties对象构建一个session

4.         用session构造消息对象

5.         设置消息内容

6.         消息对象设置发件人地址

7.         消息对象设置收件人地址

8.         连接服务器的邮箱

9.         发送邮件到目标地址

10.     关闭服务

 

 

简单示例:

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;

public class TestJavaMail {
	public static void main(String[] args) throws MessagingException {
		Properties props = new Properties();
		//设置发送邮件的邮件服务器属性
		props.put("mail.smtp.host", "smtp");
		//需要授权验证
		props.put("mail.smtp.auth", "true");
		//用properties构建一个session
		Session session = Session.getInstance(props);
		//定义消息对象
		MimeMessage message = new MimeMessage(session);
		//设置消息文本内容
		message.setContent("Hello", "text/plain");
		//地址
		Address address = new InternetAddress("XXXX@163.com");
		//发件人地址
		message.setFrom(address);
		//收件人地址
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(
				"XXXX@qq.com"));
		
		Transport transport = session.getTransport("smtp");
		//连接服务器的邮箱
		transport.connect("smtp.163.com", "XXXX", "*********");
		//发送邮件到目标地址
		transport.sendMessage(message, message.getAllRecipients());
		//关闭服务
		transport.close();

		System.out.println("发送成功!");
	}
}


 

***使用Multipart对象完成添加附件***

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值