邮件服务
邮件服务是大部分系统中不可或缺的一个服务,jfinalQ中采用mail.jar实现发送邮件功能。
QMailUtil
com.uikoo9.util.function.QMailUtil对mail.jar的常见操作做了封装,如下:
public class QMailUtil {
private static final Logger logger = LoggerFactory.getLogger(QMailUtil.class);
/**
* 发送邮件,通用
* @param mailInfo 邮件信息
* @param isHtml 是否html格式
* @return 结果
*/
public static boolean sendMail(String from, String to, String title, String content, boolean isHtml){
}
/**
* 发送邮件,普通
* @param mailInfo 邮件信息
* @return 结果
*/
public static boolean sendTextMail(String from, String to, String title, String content){
return sendMail(from, to, title, content, false);
}
/**
* 发送邮件,html
* @param mailInfo 邮件信息
* @return 结果
*/
public static boolean sendHtmlMail(String from, String to, String title, String content){
return sendMail(from, to, title, content, true);
}
/**
* 生成一个邮件消息,内部使用
* @param mailInfo 邮件信息
* @param isHtml 是否html格式
* @return 邮件消息
*/
private static Message generateMailMessage(String from, String to, String title, String content, boolean isHtml){}
}
大概就是有一个生成邮件消息方法,发送邮件(普通,html)方法。
企业邮箱
有了这个util之后,你还需要一个企业邮箱,个人推荐163的企业邮箱,使用和界面都比较简单。
然后你需要在config.properties中配置mail相关信息:
# mail properties
mail.smtp.host=smtp.ym.163.com
mail.smtp.port=25
mail.smtp.auth=true
mail.user.username=
mail.user.password=
mail.user.from=
一段示例
下面是一段发送邮箱校验的代码:
private boolean sendMail(String usermail){
try {
String title = "uikoo9.com-注册确认";
String token = QEncodeUtil.md5Encrypt(usermail + QDateUtil.format(new Date(), "yyyyMMdd"));
String url = "http://uikoo9.com/reg/regConfirm?mail=" + usermail + "&token=" + QEncodeUtil.urlEncode(token);
StringBuilder sb = new StringBuilder();
sb.append("欢迎使用“uikoo9.com”<br/><br/>");
sb.append("请点击确认链接:<a href=\"" + url + "\" target=\"_blank\">" + url + "</a><br/><br/>");
sb.append("确认链接有效期:1天<br/><br/>");
sb.append("系统邮件,请勿回复");
return QMailUtil.sendHtmlMail(from, usermail, title, sb.toString());
} catch (Exception e) {
logger.error(QStringUtil.fromException(e));
return false;
}
}
短信服务
虽然人们使用短信的频率日渐减少,但是各种系统和app中短信校验却必不可少,
与mail服务免费不同,sms服务一般都是收费的,而且都必须有企业认证,
这里推荐一个不需要企业认证的sms服务,速度还是比较快的,收费也相对较低,
中国网建sms短信服务:http://sms.webchinese.cn/Login.shtml
实现
mail服务需要单独的jar,这里只需要模仿http请求,请求服务平台的接口即可。
sms相关操作已经封装到:com.uikoo9.util.external.QSMSUtil中,
代码比较简单,就不列举了。
注意
同样的需要在config.properties中添加sms相关信息,如下:
# sms properties sms.url=http://utf8.sms.webchinese.cn/ sms.uid= sms.key= sms.to= sms.sign=
另外还有一点注意的是,sms服务一般需要你向服务商申请固定的模版,
你发送短信只能在这些模版中选择。
jfinalQ2.0
源码:https://github.com/uikoo9/jfinalQ
更多精彩内容:http://uikoo9.com/
求打赏(长按图片即可识别)~ |
捐助列表:http://uikoo9.com/donate/ |