知方号

知方号

使用java发送Email邮件(以QQ邮箱为例)

使用java发送Email邮件(以QQ邮箱为例)

 

QQ邮箱获得smtp授权码 https://service.mail.qq.com/cgi-bin/help?subtype=1&id=28&no=1001256 163邮箱获得smtp授权码 http://help.163.com/14/0923/22/A6S1FMJD00754KNP.html

 进入QQ邮箱>设置

 

 需要进行验证

 

 pom.xml 依赖坐标

com.sun.mail javax.mail 1.6.2 javax.mail javax.mail-api 1.6.2

maven项目下resources文件下新建一个applicationContext-email.xml

 代码如下:

true

在test文件下建一个TestEmail

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:applicationContext.xml")

 在java下建一个util

 

package com.study.it.util;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Component;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeUtility;import java.io.File;import java.util.ArrayList;import java.util.List;@Componentpublic class EmailUtil { private JavaMailSender sender=null; @Autowired public void setSender(JavaMailSender sender) { this.sender = sender; } /** * 发送简单的邮件 * @param to 接收人的邮箱 * @param title 邮件标题 * @param content 邮件内容 */ public void sendSimple(String to,String title,String content){ SimpleMailMessage m=new SimpleMailMessage(); m.setFrom(((JavaMailSenderImpl)sender).getUsername()); m.setTo(to); m.setSubject(title); m.setText(content); sender.send(m); System.out.println("发送成功"); } /** *发送带附件的邮件 * @param to 接收人的邮箱 * @param title 邮件标题 * @param html 邮件内容 * @param filenames 文件的List集合(字符串形式) */ public void snedMail2(String to, String title, String html, List filenames){ List files=new ArrayList(filenames.size()); for (String name:filenames) files.add(new File(name)); this.sendMail(to,title, html, files); } /** * 发送带附件的邮件 * @param to 接收人的邮箱 * @param title 邮件标题 * @param html 邮件内容 * @param files 文件的List集合 */ public void sendMail(String to, String title, String html, List files){ try { MimeMessage msg=sender.createMimeMessage(); MimeMessageHelper helper=new MimeMessageHelper(msg,true,"UTF-8"); helper.setFrom(((JavaMailSenderImpl)sender).getUsername()); helper.setTo(to); helper.setSubject(title); helper.setText(html,true); if(files!=null && files.size()>0){ for (File f:files) helper.addAttachment(MimeUtility.encodeWord(f.getName()), f); } sender.send(msg); System.out.println("发送完成"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * * 发送带单附件的邮件 * @param to 接收人的邮箱 * @param title 邮件标题 * @param html 邮件内容 * @param filename 对方显示附件的文件名 * @param filepath 文件路径 */ public void sendMail(String to, String title, String html, String filename,File filepath){ try { MimeMessage msg=sender.createMimeMessage(); MimeMessageHelper helper=new MimeMessageHelper(msg,true,"UTF-8"); helper.setFrom(((JavaMailSenderImpl)sender).getUsername()); helper.setTo(to); helper.setSubject(title); helper.setText(html,true); helper.addAttachment(MimeUtility.encodeWord(filename),filepath); sender.send(msg); System.out.println("发送完成"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * 发送内容为html格式的邮件 * @param to 接收人的邮箱 * @param title 邮件标题 * @param html 邮件内容 */ public void sendMail(String to, String title, String html){ try { MimeMessage msg=sender.createMimeMessage(); MimeMessageHelper helper=new MimeMessageHelper(msg,true,"UTF-8"); helper.setFrom(((JavaMailSenderImpl)sender).getUsername()); helper.setTo(to); helper.setSubject(title); helper.setText(html,true); sender.send(msg); System.out.println("发送完成"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }}

建一个Controller以及在配置文件配置好

ui包下面的Controller代码:

·

package com.study.it.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class EmailController { @Autowired JavaMailSender sender = null; @ResponseBody @RequestMapping(value = "/sendEmail" ,produces = "application/json;charset=utf8") public String sendMail(){ SimpleMailMessage smm = new SimpleMailMessage(); smm.setFrom("792575194@qq.com"); smm.setTo("86747505@qq.com"); smm.setSubject("你个憨憨"); smm.setText("真不错"); sender.send(smm); System.out.println("发送完成"); return "1"; }}

整个pom.xml文件

4.0.0 com.study csdn 1.0-SNAPSHOT war csdn Maven Webapp http://www.example.com UTF-8 1.7 1.7 4.3.11.Final 4.3.7.RELEASE com.sun.mail javax.mail 1.6.2 javax.mail javax.mail-api 1.6.2 org.hibernate hibernate-core ${hibernate.version} org.hibernate hibernate-ehcache 4.3.8.Final net.sf.ehcache ehcache 2.9.0 org.hibernate hibernate-c3p0 ${hibernate.version} com.mchange c3p0 0.9.5.2 mysql mysql-connector-java 8.0.16 junit junit 4.12 test com.alibaba fastjson 1.2.13 com.alibaba druid 1.1.8 log4j log4j 1.2.17 junit junit 4.12 test org.aspectj aspectjweaver 1.8.10 org.springframework spring-core ${spring.version} org.springframework spring-expression ${spring.version} org.springframework spring-beans ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-orm ${spring.version} org.springframework spring-oxm ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-webmvc-portlet ${spring.version} org.springframework spring-test ${spring.version} test com.fasterxml.jackson.core jackson-databind 2.6.1 com.fasterxml classmate 1.3.3 javax.validation validation-api 1.1.0.Final org.hibernate hibernate-validator 5.3.5.Final mysql mysql-connector-java 8.0.22 com.mchange c3p0 0.9.5.2 cglib cglib 3.1 org.javassist javassist 3.17.1-GA javax.servlet javax.servlet-api 3.1.0 javax.servlet.jsp javax.servlet.jsp-api 2.2.1 jstl jstl 1.2 commons-fileupload commons-fileupload 1.3.1 commons-lang commons-lang 2.6 org.apache.commons commons-lang3 3.5 commons-io commons-io 2.5 commons-collections commons-collections 3.2.1 net.sf.ehcache ehcache-core 2.6.9 com.github.penggle kaptcha 2.3.2 commons-httpclient commons-httpclient 3.1 commons-collections commons-collections 3.2.1 net.sf.ehcache ehcache-core 2.6.9 org.apache.shiro shiro-spring 1.2.3 org.apache.shiro shiro-ehcache 1.2.3 org.apache.shiro shiro-quartz 1.2.3 com.oracle ojdbc6 11.2.0.3 com.sun.mail javax.mail 1.6.2 javax.mail javax.mail-api 1.6.2 csdn maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。