声明:涉及到spring的bean加载的内容,这里不做叙述。
一、实现邮件发送之前,现在.xml文件中进行配置:
2种方式:(1)QQ邮箱:
<bean id="sm" class="cn.redinfo.jlxh.manager.bean.SendMail"> <property name="host" value="smtp.exmail.qq.com" /> <property name="port" value="25" /> <property name="address" value="123456789@qq.com" /> <property name="password" value="2017" /> </bean>
(2)其他邮箱发送:
<bean id="sm" class="simple.threads.SendMail"> <property name="host" value="smtp.163.com" /> <property name="port" value="25" /> <property name="address" value="666666@163.com" /> <property name="password" value="123456" /> </bean> //定时发送 <bean id="dataThread2" class="simple.threads.ScannCheckStatusThread" factory-method="getInstance" init-method="init"> <property name="sendMail" ref="sm"/> <property name="startTime" value="22:00:00" /> //每天晚上10整 <property name="nextTime" value="24*60*60*1000" /> //每隔一天发送一次 </bean>
host=smtp.ym.163.com
java中实现邮件的发送:
/** * 线程:发送提醒邮件 * * @author Administrator * */ public class SendMail { private String host; private String port; private String address; private String password; public void process(String mailContent, String receiveAddress) { try { MailSenderInfo mailInfo = new MailSenderInfo(); //自己封装的类 mailInfo.setMailServerHost(this.host); mailInfo.setMailServerPort(this.port); mailInfo.setValidate(true); mailInfo.setUserName(this.address); mailInfo.setPassword(this.password);// 您的邮箱密码 mailInfo.setFromAddress(this.address); mailInfo.setSubject("发送通知"); SimpleMailSender sms = new SimpleMailSender(); //自己封装的类 mailInfo.setToAddress(receiveAddress); mailInfo.setContent(mailContent); sms.sendTextMail(mailInfo);// 发送文体格式 } catch (Exception e) { e.printStackTrace(); } } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
实际应用:
StringBuffer sb = new StringBuffer();
sb.append("您好:"+"\n"); sb.append(" 您报名参加的专业的培训班已经开班,现将培训通知发给您,请按时报到。\n"); sb.append(" 培训部025-88888888"); SendMail send = new SendMail();
send.process(sb.toString(), "6666666@qq.com"); //发送邮件提醒