利用javamail发送邮件实例

最近在学习使用javamail发送邮件

使用javamail发送邮件,要先导入activation.jar,mail.jar两个包

使用的sina邮箱往163的两个邮箱发送邮件

下面是简单的测试源码

 

MailConfig .java代码 

package com.test;

public class MailConfig {
	
	private String from;//发件人
	private String to;//收件人
	private String host;//发送邮件服务器
	private String password;//密码
	
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

 

SendMail .java

package com.test;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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.MimeMultipart;

public class SendMail {
	
	public void sendMail(String subject,String content,MailConfig mailConfig,String fileNames){
		Properties props = new Properties();
		props.put("mail.smtp.host", mailConfig.getHost());
		
		Session session = Session.getDefaultInstance(props,null);
		Message message = new MimeMessage(session);
		
		try{
			message.setFrom(new InternetAddress(mailConfig.getFrom()));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailConfig.getTo()));
			message.setSubject(subject);
			
			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setText(content);
			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(bodyPart);
			
			if(!(fileNames==null||fileNames.length()==0)){
				String [] s = fileNames.split(",");
				for(String fileName:s){
					BodyPart messageBodyPart = new MimeBodyPart();
					DataSource dataSource = new FileDataSource(fileName);
					messageBodyPart.setDataHandler(new DataHandler(dataSource));
					messageBodyPart.setFileName(dataSource.getName());
					multipart.addBodyPart(messageBodyPart);
				}
			}
			
			message.setContent(multipart);
			Transport transport = session.getTransport("smtp");
			transport.connect(mailConfig.getHost(), mailConfig.getFrom(), mailConfig.getPassword());
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}


MailTest.java

package com.test;

public class MailTest {
	
	public static void main(String[] args) {
		
		String subject = "测试";
		String content = "邮件测试,带附件的测试!";
		
		MailConfig mailConfig = new MailConfig();
		mailConfig.setHost("smtp.sina.com.cn");//smtp服务器,我这用的是sina的smtp
		mailConfig.setFrom("fengjiang1017@sina.com");//发件人邮箱
		mailConfig.setTo("yang_fengjiang@163.com,snrqtdhuqf@163.com");//多个收件人使用逗号分隔
		mailConfig.setPassword("xxxxxx");//发件人密码
		
		SendMail mail = new SendMail();
		
		//这是带附件的邮件发送,如果没有邮件,可以置fileNames=""或者fileNames=null
		String fileNames = "d:/sof.sql,d:/myeclipse_templates_8.5.rar";
		mail.sendMail(subject, content, mailConfig, fileNames);
		
		System.out.println("发送成功!");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值