关闭

javamail发送与接收邮件实例

标签: spring邮件smtp
841人阅读 评论(0) 收藏 举报
分类:
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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:94646次
    • 积分:1267
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:17篇
    • 译文:0篇
    • 评论:13条
    最新评论