JSP怎么实现自动发送邮件?请详解!高分 会员注册成功后自动邮件回复! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 google一下“java 邮件 发送”,能找到很多方法 用javamail类import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class MailBean { public int sendMail(String userId) throws SQLException{ int result = 0; ClassmateBean classmateBean = new ClassmateBean(); ClassmateVO classmateVO = classmateBean.getClassmateInfo(userId); String mail1 = Tools.makeMailAddress(classmateVO.getMail1Head(),classmateVO.getMail1Body()); if(mail1.equals("")){ return 1; } String msgText = ""; String to = mail1; String from = Tools.getProperty("master.mailaddress"); String host = Tools.getProperty("master.mailserver"); Properties props = new Properties(); ・ props.put("mail.smtp.host", host); props.put("mail.smtp.auth","true"); MailAuthenticator authenticator = new MailAuthenticator( Tools.getProperty("master.mailuser"), Tools.getProperty("master.mailpassword")); Session session = Session.getDefaultInstance(props,authenticator); try { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("ウ、フリヒトヨミヘィヨェ"); msg.setSentDate(new Date()); msg.setText(msgText);  Transport.send(msg); }catch (MessagingException mex) { mex.printStackTrace(); result = 1; } return result; }}这是我以前写的类,至于中间那些读属性文件的操作,你就不用管了,反正都是字符串。至于javamail的类包,你自己从网上下载吧。 en javamail 一般都用这个吧 public class EMail { Properties props = new Properties(); Session sendMailSession; Store store; Transport transport; String usr = ""; //这里填写你发信者的邮箱地址 String pwd = ""; //这里填写你发信者的邮箱密码 String smtpServer = ""; //这里填写smtp服务器的名称 String mailContent = ""; //这里填写邮件内容 String toAddress = ""; //这里填写收件人的地址; String fromAddress = ""; //这里填写发件人的地址 String mailSubject = ""; //填写邮件的主题 public EMail() { } public boolean Init() { try { Properties p = new Properties(); String fileName = "/ApplicationResources.properties"; //指定资源文件 InputStream is = getClass().getResourceAsStream(fileName); //打开资源文件 p.load(is); //装载资源文件 this.smtpServer=p.getProperty("mail.smtp.host"); this.usr=p.getProperty("mail.from.user"); this.pwd=p.getProperty("mail.from.psw"); this.fromAddress=p.getProperty("mail.from.address"); } catch (Exception ex) { return false; } return true; } public boolean SendMail() { try { props.put("mail.smtp.host",smtpServer); //这里填写你发信者的SMTP主机,如:smtp.sohu.com props.put("mail.smtp.user", usr); props.put("mail.smtp.password", pwd); props.put("mail.smtp.auth", "true"); sendMailSession = Session.getInstance(props, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(usr,pwd); } //这里填写你发信者的邮箱地址和密码 }); //如果邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。 Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(fromAddress)); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); newMessage.setSubject(mailSubject); newMessage.setSentDate(new Date()); newMessage.setText(mailContent); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); } catch (Exception m) { m.printStackTrace(); return false; } return true; } mport java.util.Properties;import javax.mail.*;import javax.mail.internet.*; public class MailExample {public static void main (String args[]) throws Exception ,MessagingException{String host = "smtp.163.com";String from = "[email protected]";String to = "[email protected]";// new propertiesProperties props = new Properties();// Setup mail serverprops.put("mail.smtp.host", host);//设置smtp主机props.put("mail.smtp.auth","true");//使用smtp身份验证// Get sessionSession session = Session.getDefaultInstance(props, null);// Define messageMimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress(from));message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));message.setSubject("Hello JavaMail");message.setText("This is test JavaMail");message.saveChanges();// Send messageTransport transport = session.getTransport("smtp");System.out.println("正在连接");transport.connect(host, "用户名","密码");System.out.println("正在发送");transport.sendMessage(message, message.getAllRecipients());System.out.println("邮件发送成功");}} 要实现自动发送邮件,那需要用到 Timer , TimerTask ,因为邮件发送程序是定时执行的,或者间隔一个固定的时间执行的。其实,发送的具体做法楼上的各位已经说的很清楚了,其实你要做的就是 ,让你的发送邮件的类 继承 TimerTask 并实现 run 方法,然后使用 Timer 把它进行“包装”,就可以实现定期发送邮件的功能。对于程序的首次出发,你可以用 servlet 来实现,在Tomcat启动的时候,让容器来除法这个定时程序,然后每间隔一定的时间,自动执行一次。当然,不光可以用 Timer , TimerTask 来实现,方法很多,但是思想都差不多。 求助 SHH整合错误,求大神指点 新手遇到问题求高手解答,在线等 小生不才,请大家罗列一下数据库连接池的集中方式 这段代码有问题吗??———jsp购书网站开发 在使用freemarker生成了html静态网页后不在访问,报404错误 求jsp带菜单的框架 jsp 中得乱码问题 有一问:如何在JSP中输入中文 【求助】macOS 10.15下MyEclipse 2017 Ci无法正常启动Tomcat服务器 大家救我,一个tomcat5的问题,对高手应该不难的 急等啊,一个ORACLE的数据文件更新问题!!!!!!!!
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class MailBean { public int sendMail(String userId) throws SQLException{
int result = 0;
ClassmateBean classmateBean = new ClassmateBean();
ClassmateVO classmateVO = classmateBean.getClassmateInfo(userId);
String mail1 = Tools.makeMailAddress(classmateVO.getMail1Head(),classmateVO.getMail1Body());
if(mail1.equals("")){
return 1;
}
String msgText = "";
String to = mail1; String from = Tools.getProperty("master.mailaddress");
String host = Tools.getProperty("master.mailserver");
Properties props = new Properties();
・
props.put("mail.smtp.host", host); props.put("mail.smtp.auth","true");
MailAuthenticator authenticator = new MailAuthenticator(
Tools.getProperty("master.mailuser"),
Tools.getProperty("master.mailpassword"));
Session session = Session.getDefaultInstance(props,authenticator);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("ウ、フリヒトヨミヘィヨェ");
msg.setSentDate(new Date());
msg.setText(msgText); 
Transport.send(msg);
}catch (MessagingException mex) {
mex.printStackTrace();
result = 1;
} return result;
}}这是我以前写的类,至于中间那些读属性文件的操作,你就不用管了,反正都是字符串。至于javamail的类包,你自己从网上下载吧。
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport; String usr = ""; //这里填写你发信者的邮箱地址
String pwd = ""; //这里填写你发信者的邮箱密码
String smtpServer = ""; //这里填写smtp服务器的名称
String mailContent = ""; //这里填写邮件内容
String toAddress = ""; //这里填写收件人的地址;
String fromAddress = ""; //这里填写发件人的地址
String mailSubject = ""; //填写邮件的主题 public EMail() { } public boolean Init() {
try {
Properties p = new Properties();
String fileName = "/ApplicationResources.properties"; //指定资源文件
InputStream is = getClass().getResourceAsStream(fileName); //打开资源文件
p.load(is); //装载资源文件
this.smtpServer=p.getProperty("mail.smtp.host");
this.usr=p.getProperty("mail.from.user");
this.pwd=p.getProperty("mail.from.psw");
this.fromAddress=p.getProperty("mail.from.address");
} catch (Exception ex) {
return false;
}
return true;
} public boolean SendMail() {
try { props.put("mail.smtp.host",smtpServer); //这里填写你发信者的SMTP主机,如:smtp.sohu.com
props.put("mail.smtp.user", usr);
props.put("mail.smtp.password", pwd);
props.put("mail.smtp.auth", "true");
sendMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(usr,pwd);
} //这里填写你发信者的邮箱地址和密码
}); //如果邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(fromAddress));
newMessage.setRecipient(Message.RecipientType.TO,
new
InternetAddress(toAddress));
newMessage.setSubject(mailSubject);
newMessage.setSentDate(new Date());
newMessage.setText(mailContent);
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
} catch (Exception m) {
m.printStackTrace();
return false;
}
return true;
}
import javax.mail.*;
import javax.mail.internet.*;
public class MailExample {
public static void main (String args[]) throws Exception ,MessagingException{
String host = "smtp.163.com";
String from = "[email protected]";
String to = "[email protected]";
// new properties
Properties props = new Properties();// Setup mail server
props.put("mail.smtp.host", host);//设置smtp主机props.put("mail.smtp.auth","true");//使用smtp身份验证// Get session
Session session = Session.getDefaultInstance(props, null);// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("This is test JavaMail");
message.saveChanges();// Send message
Transport transport = session.getTransport("smtp");
System.out.println("正在连接");
transport.connect(host, "用户名","密码");
System.out.println("正在发送");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("邮件发送成功");
}
}
那需要用到 Timer , TimerTask ,
因为邮件发送程序是定时执行的,或者间隔一个固定的时间执行的。其实,发送的具体做法楼上的各位已经说的很清楚了,
其实你要做的就是 ,让你的发送邮件的类 继承 TimerTask 并实现 run 方法,
然后使用 Timer 把它进行“包装”,就可以实现定期发送邮件的功能。对于程序的首次出发,你可以用 servlet 来实现,在Tomcat启动的时候,让容器来除法这个定时程序,然后每间隔一定的时间,自动执行一次。当然,不光可以用 Timer , TimerTask 来实现,方法很多,但是思想都差不多。