67,512
社区成员
发帖
与我相关
我的任务
分享
package com.cc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
public class TestEmail {
public static JavaMailSender sender;
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
sender = (JavaMailSender) ctx.getBean("mailSender");
//测试发送只有文本信息的简单测试
sendText("aaaaa@qq.com","邮件~~~~~~~~~~~","收到了没~收到回下~");
}
public static void sendText(String recipient, String subject, String content)
throws MessagingException
{
MimeMessage mimeMsg = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");
helper.setTo(recipient);
helper.setFrom("bbbbbb@icgd.cn");
helper.setSubject(subject);
helper.setText(content);
sender.send(mimeMsg);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.exmail.qq.com" />
<property name="username" value="123456.cn" />
<property name="password" value="123456" />
<property name="port" value="465" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">60000</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
</props>
</property>
</bean>
</beans>
我这里有个例子你参考一下,或许对你有用
1.java文件
package com.soft.mail;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class MailService {
private MailSender mailSender;
private SimpleMailMessage mailMessage;//邮件模板
public void sendMail() {
System.out.println("-----------发送邮件!---------");
SimpleMailMessage msg=new SimpleMailMessage(this.mailMessage);
msg.setText("this is a test mail");
try {
mailSender.send(msg);
} catch (MailException e) {
e.printStackTrace();
}
System.out.println("-----------发送成功!---------");
}
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public SimpleMailMessage getMailMessage() {
return mailMessage;
}
public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}
}
2.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com"/>
<!-- 有些邮件服务器发送邮件需要认证,所以必须提供帐号 -->
<property name="username" value="xinghui_liu1@163.com"/>
<property name="password" value=""/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to" value="ling67614@163.com"></property>
<property name="from" value="xinghui_liu1@163.com"></property>
<property name="subject" value="邮件测试"></property>
</bean>
<bean id="ms" class="com.soft.mail.MailService">
<property name="mailSender" ref="mailSender"></property>
<property name="mailMessage" ref="mailMessage"></property>
</bean>
<bean id="reportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="ms"></property>
<property name="targetMethod" value="sendMail"></property>
</bean>
<!-- 一个简单的调度器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="reportJob"></property>
<property name="startDelay" value="1"></property>
<property name="repeatInterval" value="1000"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>
</beans>