邮箱批量注册(outlook邮箱批量注册)

如何使用java实现邮件发送

首先引入所需的jar包

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

</dependency>

<dependency>

<groupId>javax.mail</groupId>

<artifactId>mail</artifactId>

<version>1.4.7</version>

</dependency>

创建mail实体类

package com.aaa.commons.mail;

/**

* @author

* @description 邮件实体类

* @company

* 2017-12-20上午9:26:20

*/

public class MailModel {

private String from;

private String to;

private String subject;

private String content;

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 getSubject() {

return subject;

}

public void setSubject(String subject) {

this.subject = subject;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public MailModel( String to, String subject, String content) {

super();

this.to = to;

this.subject = subject;

this.content = content;

}

public MailModel() {

super();

// TODO Auto-generated constructor stub

}

}

创建mail业务类

package com.aaa.commons.mail;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import org.springframework.stereotype.Service;

/**

* @author TeacherChen

* @description 实现邮件发送

* @company

* 2017-12-20上午9:27:39

*/

@Service(“mailBiz”)

public class MailBiz {

@Autowired

private JavaMailSender mailSender;

@Autowired

private SimpleMailMessage simpleMailMessage;

//注入线程池对象

@Autowired

private ThreadPoolTaskExecutor taskExecutor;

/**

* 发送邮件

*/

public void sendMail(final MailModel mm) {

taskExecutor.execute(new Runnable() {

@Override

public void run() {

try {

/*simpleMailMessage.setFrom(mm.getFrom());//设置发件人

*/simpleMailMessage.setTo(mm.getTo());//设置收件人

simpleMailMessage.setSubject(mm.getSubject());//主题

simpleMailMessage.setText(mm.getContent());//内容

mailSender.send(simpleMailMessage);

} catch (Exception e) {

System.out.println(“邮件发送异常”);

e.printStackTrace();

}

System.out.println(“邮件发送成功!”);

}

});

}

}

测试类

package com.aaa.mail;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aaa.commons.mail.MailBiz;

import com.aaa.commons.mail.MailModel;

public class SendMailTest {

/**

* @param args

*/

public static void main(String[] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext(“classpath:spring/applicationContext.xml”);

MailBiz mb=(MailBiz) ac.getBean(“mailBiz”);

MailModel mm = new MailModel( “1580496969@qq.com”, “xxxxxxxxxxx”, “ooooooooo”);

mb.sendMail(mm);

}

}

配置文件

#SMTP服务配置

mail.host=smtp.qq.com

mail.port=465

mail.username=86521760@qq.com

##qq邮箱必须使用ssl授权码

mail.password=保密,自己申请

mail.smtp.auth=true

mail.smtp.timeout=30000

mail.default.from=86521760@qq.com

#STARTTLS是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL)

mail.smtp.starttls.enable=true

<!– Spring提供的发送电子邮件的高级抽象类 –>

<bean id=“mailSender” class=“org.springframework.mail.javamail.JavaMailSenderImpl”>

<property name=“host” value=“${mail.host}” />

<property name=“username” value=“${mail.username}” />

<property name=“password” value=“${mail.password}” />

<property name=“port” value=“${mail.port}” />

<property name=“defaultEncoding” value=“UTF-8”></property>

<property name=“javaMailProperties”>

<props>

<prop key=“mail.smtp.auth”>${mail.smtp.auth}</prop>

<prop key=“mail.smtp.timeout”>${mail.smtp.timeout}</prop>

<prop key=“mail.smtp.starttls.enable”>${mail.smtp.starttls.enable}</prop>

<prop key=“mail.smtp.socketFactory.port”>${mail.port}</prop>

<prop key=“mail.smtp.socketFactory.class”>javax.net.ssl.SSLSocketFactory</prop>

<prop key=“mail.smtp.socketFactory.fallback”>false</prop>

</props>

</property>

</bean>

<bean id=“simpleMailMessage” class=“org.springframework.mail.SimpleMailMessage”>

<property name=“from”>

<value>${mail.default.from}</value>

</property>

</bean>

本文来自投稿,仅供学习参考!不代表本站立场,该文观点仅代表原作者本人,本站不拥有所有权,不承担相关法律责任。如发现本站有抄袭侵权/违规的内容,请发送邮件至83771837@qq.com举报,一经查实,本站将立刻删除。