javamail发送与接收邮件实例

1、如果是maven项目,需要引入依赖包
<dependency>  
    <groupId>javax.mail</groupId>  
    <artifactId>mail</artifactId>  
    <version>1.4.4</version>  
</dependency> 

2、spring applicationContext.xml配置文件中java mail的配置如下所示:
<!-- simple -->
<bean id="javaMailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
       <property name="host" value="${simple.email.host}"/>  
       <property name="port" value="${simple.email.port}"/>  
       <property name="protocol" value="${simple.email.protocol}"/>  
       <property name="username" value="${simple.email.username}"/>  
       <property name="password" value="${simple.email.password}"/>  
       <!-- SMTP服务器验证 -->  
       <property name="javaMailProperties">  
           <props>  
               <!-- 验证身份 -->  
               <prop key="mail.smtps.auth">true</prop>  
               <prop key="mail.transport.protocol">smtp</prop>  
          </props>  
       </property>  
</bean>  
     
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">  
    <property name="from">  
        <value>${simple.email.username}</value>  
    </property>  
    <property name="to">  
        <value>${simple.email.receiver}</value>  
    </property>  
   </bean>
</beans>

3、java mail发送与接收邮件的代码如下:
package tv.bgsc.paymentplatform.mail.test;

import java.io.File;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/simple/test")
public class SimpleEmailTest {

	@Resource
	private JavaMailSenderImpl javaMailSenderImpl;
	
	@Resource
	private SimpleMailMessage  simpleMailMessage;
	
	@RequestMapping(value="/send")
	@ResponseBody
	public String simpleEmailSender(@RequestParam String text,@RequestParam String title){
		try{
			this.simpleMailMessage.setText(text);
			this.simpleMailMessage.setSubject(title);
			this.javaMailSenderImpl.send(simpleMailMessage);
		}catch(Exception ex){
			ex.printStackTrace();
			return "error";
		}
		return "success";
	}
	
	@RequestMapping(value="/multipart")
	@ResponseBody
	public String multipartEmailSender(@RequestParam String text,@RequestParam String title){
		try{
			MimeMessage message=this.javaMailSenderImpl.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");
			helper.setTo(this.simpleMailMessage.getTo());
			helper.setFrom(this.simpleMailMessage.getFrom());
			helper.setSubject("发送多个附件数据");
			helper.setText("后台自动生成excel的数据文档");
			
			FileSystemResource file = new FileSystemResource(new File("C:/Users/Administrator/Desktop/333222.txt"));
			helper.addAttachment("txt文档", file);
			
			FileSystemResource file2 = new FileSystemResource(new File("C:/Users/Administrator/Desktop/12122.xls"));
			helper.addAttachment("excel数据文档", file2);
			
			javaMailSenderImpl.send(message);
			
		}catch(Exception ex){
			ex.printStackTrace();
			return "error";
		}
		
		return "success";
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值