您的位置:首页 > 编程语言 > Java开发

JavaMail发送邮件

2013-03-21 15:32 211 查看
最近用到邮件发送功能,就搜了一下文章,看了下,当然也可以直接看javamail的帮助文档。如果找到一篇好文章,上手还是挺快的,这有篇关于javamail的不错文章,推荐下:

http://blog.csdn.net/chjttony/article/details/6005235。好东西就要分享,呵呵...

发送纯文本或html格式的邮件

public boolean sendEmail(String propsFile, String to, String title,String content,boolean isHtml) {
		//创建邮件配置属性
		Properties props = new Properties();
		boolean result = false;
		try {
			//邮件服务器地址
			String host = PropertiesUtil.getProperties("mail.smtp.host", propsFile);
			//发件人邮箱登陆账号
			String username = PropertiesUtil.getProperties("username", propsFile);
			//发件人邮箱登陆密码(加密后的,需要解密)
			String password = PropertiesUtil.getProperties("password", propsFile);
			//发件人邮箱
			String from = PropertiesUtil.getProperties("from", propsFile);
			
			/*1.设置连接会话属性*/
			//邮件发送服务器地址
			props.put("mail.smtp.host",host );
			//连接邮件服务器时是否验证
			props.put("mail.smtp.auth", "true");
			//创建邮件会话
			Session session = Session.getInstance(props);
			//设置调试模式,可以打印邮件发送过程信息
			session.setDebug(true);
			
			/*2.创建邮件对象*/
			//由会话对象创建消息对象
			Message message = new MimeMessage(session);
			
			/*3.设置邮件相关信息*/
			//发件人地址
			InternetAddress fromAddress = new InternetAddress(from);
			//设置发件人地址
			message.setFrom(fromAddress);
			//设置收件人
			InternetAddress toAddress = new InternetAddress(to);
			//设置收件人接收类型为TO
			message.setRecipient(Message.RecipientType.TO, toAddress);
			//设置邮件主题
			message.setSubject(title);
			//设置邮件内容
			if(isHtml){
				//html格式
				message.setContent(content,"text/html;charset=gb2312");
			}else{
				//纯文本
				message.setText(content);
			}
			//设置邮件发送时间
			message.setSentDate(new Date());
			
			/*4.发送邮件*/
			//存储邮件信息
			message.saveChanges();
			//创建邮件发送对象,并指定发送协议为SMTP
			Transport transport = session.getTransport("smtp");
			//登陆邮箱服务器
			transport.connect(host, username, CyptUtil.decrypt(password));
			//发送邮件
			transport.sendMessage(message, message.getAllRecipients());
			//关闭连接
			transport.close();
			result = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return result;
	}
其中PropertiesUtil和CyptUtil是两个工具类,前者是用来从property配置文件读取邮件服务器配置参数,代码中可以直接替换为自己的配置选项。CyptUtil是用来加密和解密的,密码当然需要加密,此处可以直接去掉CyptUtil.decrypt()方法,使用明文密码.

发送带附件的邮件:

public boolean sendEmailWithMultipart(String propsFile, String to,
			String title, String content, String filename) {
		boolean result = false;
		Properties props = new Properties();
		try {
			String host = PropertiesUtil.getProperties("mail.smtp.host", propsFile);
			String username = PropertiesUtil.getProperties("username", propsFile);
			String password = PropertiesUtil.getProperties("password", propsFile);
			String from = PropertiesUtil.getProperties("from", propsFile);
			//1.设置属性
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.auth", true);
			
			//2.创建会话
			Session session = Session.getInstance(props);
			session.setDebug(true);
			
			//3.创建消息体(HTML格式发送邮件内容)
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			message.setSubject(title);
			
			//4.创建附件内容对象(mime类型邮件)
			//bodyPart对象是用来存放信件内容的
			BodyPart textBodyPart = new MimeBodyPart(); //存放文本内容
			textBodyPart.setContent(content, "text/html;charset=gb2312");
			BodyPart messageBodyPart = new MimeBodyPart(); //存放附件内容
			
			//5.设置附件内容对象的数据源和dataHandler
			//建立附件数据源,如果是通过url传输,则需要使用UrlDataSource(url)
			DataSource source = new FileDataSource(filename);
			//设置附件的DataHandler
			messageBodyPart.setDataHandler(new DataHandler(source));
			//设置附件的名称
			messageBodyPart.setFileName(filename);
			
			//6.创建附件对象
			//新建一个附件对象用来存放一个或多个BodyPart对象
			Multipart multipart = new MimeMultipart();
			//将附件内容对象加入到附件对象中
			multipart.addBodyPart(textBodyPart);
			multipart.addBodyPart(messageBodyPart);
			
			//7.添加附件对象到消息体
			message.setContent(multipart);
			message.setSentDate(new Date());
			message.saveChanges();
			
			//8.发送邮件
			Transport transport = session.getTransport("smtp");
			transport.connect(host, username, CyptUtil.decrypt(password));
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			result = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return result;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: