用 play framework 发送邮件,为什么收不到邮件?

1.我在开发一个网站
2.遇到的问题:接受邮箱接受不到邮件
3.想实现的功能:

点击页面上“发送邮件提醒”按钮,按照配置好的信息(发送邮箱,发送邮箱密码,接受邮箱等)发送出一封邮件。

我的实现代码:
1.配置文件

conf/application.conf #发送邮件配置
mail.smtp.host=smtp.qq.com
mail.smtp.user=82539****
mail.smtp.pass=abc******
mail.debug=true

2.controllers/Application.class

 public static void sendEmail{ //Mail.send"59******@qq.com", "890*****@qq.com", "主题", "内容";//行不通 SimpleEmail email = new SimpleEmail; try{ email.setFrom"1205******@qq.com"; email.addTo"8253******@qq.com"; email.setSubject"轮值提醒-lyd"; email.setMsg"轮值啦!-lyd"; Mail.sendemail; String msg = "发送成功!"; render"/Application/showMsg.html",msg; }catch Exception e{ } //String msg = "发送成功!"; //render"/Application/showMsg.html",msg; }

3.views/Application/sendEmail.html

<form action="@{Application.sendEmail}" class="form-btn"> <button id="snedEmail" type="submit" value = "发送邮件提醒"></button>
</form>

4.控制台信息

但是邮箱就是没有新邮件。

尝试第二种方法还是失败,邮箱没有收到任何邮件
实现代码:
1.配置文件同上
2.notifiers/Mails.java 创建Mailer的子类Mails,放在包notifiers里

 package notifiers; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.SimpleEmail; import play.libs.Mail; import play.mvc.Mailer; /* Created with IntelliJ IDEA. User: afra Date: 16-3-1 Time: 下午6:58 To change this template use File | Settings | File Templates. */ public class Mails extends Mailer { public static void welcome { setSubject"Welcome !!!"; addRecipient"12******@qq.com"; setFrom"882******@qq.com"; send; } }

3.调用代码(controllers/Application.java)

public static void sendEmail{ Mails.welcome; }

4.对应HTML的邮件页面:Mails/welcome.html

!DOCTYPE html>
<html>
<head> <title></title>
</head>
<body>
hello word!
</body>
</html>

5.页面上按钮“发送邮件提醒”的代码同上。
6.控制台输出信息:

7.邮箱没有反应,收不到邮件。尝试了qq邮箱和163邮箱都不行。

用Play framework发送邮件步骤总结

1.配置文件application.conf
将配置文件中mail.smtp=mock注释掉(该行配置的意思是模拟发送,只在控制台上输出信息,并不会真正发送邮件)


2.配置文件application.conf配置如下信息:

mail.smtp.host=smtp.cstnet.cn
mail.smtp.user=发送方@hpnl.ac.cn
mail.smtp.pass=密码
mail.debug=true

注意:qq邮箱等需要认证等额外配置,我使用的校邮箱不用认证,邮箱服务器mail.smtp.host可以去邮件系统的设置里面查看到
2.新建包notifiers,写一个Mailer的子类Mails.java

package notifiers; import models.User;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.SimpleEmail;
import play.libs.Mail;
import play.mvc.Mailer; import java.util.List;
public class Mails extends Mailer { public static void welcome { setSubject"Welcome !!!"; addRecipient"接收方@qq.com"; setFrom"发送方@hpnl.ac.cn"; send; } public static void sendEmailForLunzhiSturdentList<User> studentList,String date, String task, int hours{ User student; setFrom"发送方@hpnl.ac.cn"; forint i = 0; i < studentList.size; i++{ student = studentList.geti; ifstudent.EmailAdress != null{ addRecipientstudent.EmailAdress; } } setSubject"您收到了新的任务"; senddate, task, hours; } public static void sendEmailForReleaseTeacherUser releaseTeacher,List<User> studentList,String date, String task, int hours{//老师 setFrom"发送方@hpnl.ac.cn"; ifreleaseTeacher.EmailAdress != null{ addRecipientreleaseTeacher.EmailAdress; setSubjectreleaseTeacher.name+"您发布了新的任务"; sendreleaseTeacher,studentList,date, task, hours; } } public static void sendEmailForAdminUser admin,User releaseTeacher,List<User> studentList,String date, String task, int hours{//管理员 setFrom"发送方@hpnl.ac.cn"; ifadmin.EmailAdress != null{ addRecipientadmin.EmailAdress; setSubjectreleaseTeacher.name+"发布了新任务。"; sendadmin,studentList,releaseTeacher,date, task, hours; } }
}

3.新建邮件内容的HTML模板,放到view/Mails文件夹下。Mails文件夹对应Mails.java类,HTML模板名对应类的方法名。

<!DOCTYPE html>
<html>
<head> <title></title>
</head>
<body>
邮件内容
</body>
</html>

4.调用

Mails.sendEmailForLunzhiSturdentstudentList,lunzhiDate, lunzhiTask, lunzhiHours;
Mails.sendEmailForReleaseTeacherreleaseTeacher, studentList, lunzhiDate, lunzhiTask, lunzhiHours;

5.未解决问题:

1.没有抄送功能,可以改用java mail
2.没有解决qq邮箱等认证的问题

发表评论

电子邮件地址不会被公开。 必填项已用*标注