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";
}
}
javamail发送与接收邮件实例
最新推荐文章于 2024-05-18 23:12:11 发布