jsp中jsp中群发邮件群发邮件
生活随笔
收集整理的這篇文章主要介紹了
jsp中jsp中群发邮件群发邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
jsp中群發郵件 1、獲取需要的jar文件,mail.jar([url]http://java.sun.com/products/javamail/[/url])和activation.jar([url]http://java.sun.com/products/javabeans/glasgow/jaf.html[/url]),將activation.jar和mail.jar復制到應用程序的\WEB-INF\lib中,即可使用。 2、sendmail.jsp文件代碼:
<% @page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.io.*,java.util.*,java.text.*, javax.mail.*, javax.mail.internet.*" %>
<jsp:useBean id="connBean" scope="session" class="opendb.opendb"/><html>
<head>
<title>JavaMail 電子郵件發送系統</title>
</head>
<body> <%
?String userName = (String)session.getAttribute("userName");
?String figure = (String)session.getAttribute("figure");
?String from = "";
?String to = "";
?ResultSet rs = null;
?String smtphost = "smtp.yeah.net"; // 發送郵件服務器
?String user = "zyf0808"; // 填寫自己的郵件服務器登錄用戶名
?String password = "123456789"; // 填寫你自己的郵件服務器登錄密碼
?String subject = connBean.change(request.getParameter("txtSubject")); // 郵件標題
?String body =connBean.change(request.getParameter("txtContent"));// 郵件內容
??? if(userName!=null && !userName.equals(""))
??{
?????ResultSet rst = connBean.ExecuteQuery("SELECT Email FROM Classmates WHERE UserName = '"+userName+"' ");
?????if (rst.next())
??????from = rst.getString("Email");????? //發件人地址
?????else from = "[email]zyf0808@yeah.net[/email]";? //換成可用的管理員的郵箱地址
??}
?else
??{
???//未登錄
%>
<SCRIPT language="JavaScript">
?? alert("對不起,您尚未登錄,只有登錄的管理員同學才能使用該功能!")
?? window.location.replace("../login.jsp");
</SCRIPT>
<%?
??}
?//得到除當前登錄管理員外的所有用戶的郵箱
?rs = connBean.ExecuteQuery("SELECT Email FROM Classmates WHERE UserName <> '"+userName+"'");
??? while (rs.next())
?{
??to = rs.getString("Email"); // 接受人郵件地址
?? if (to !=null && !to.equals("") )
??{
??try
??{
???Properties props = new Properties();
???props.put("mail.smtp.host", smtphost);
???props.put("mail.smtp.auth","true");
???Session ssn = Session.getInstance(props, null); MimeMessage message = new MimeMessage(ssn); InternetAddress fromAddress = new InternetAddress(from);
???message.setFrom(fromAddress);
???InternetAddress toAddress = new InternetAddress(to);
???message.addRecipient(Message.RecipientType.TO, toAddress); message.setSubject(subject);
???message.setText(body); Transport transport = ssn.getTransport("smtp");
???transport.connect(smtphost, user, password);
???transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
???transport.close();
??}
??catch(Exception e)
???{
????out.println(e.toString());
???}
??}
?}
%>
<SCRIPT language="JavaScript">
?? alert("郵件已經成功發送!")
?? window.location.replace("../index.jsp");
</SCRIPT>
</BODY>
</HTML>
<% @page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.io.*,java.util.*,java.text.*, javax.mail.*, javax.mail.internet.*" %>
<jsp:useBean id="connBean" scope="session" class="opendb.opendb"/><html>
<head>
<title>JavaMail 電子郵件發送系統</title>
</head>
<body> <%
?String userName = (String)session.getAttribute("userName");
?String figure = (String)session.getAttribute("figure");
?String from = "";
?String to = "";
?ResultSet rs = null;
?String smtphost = "smtp.yeah.net"; // 發送郵件服務器
?String user = "zyf0808"; // 填寫自己的郵件服務器登錄用戶名
?String password = "123456789"; // 填寫你自己的郵件服務器登錄密碼
?String subject = connBean.change(request.getParameter("txtSubject")); // 郵件標題
?String body =connBean.change(request.getParameter("txtContent"));// 郵件內容
??? if(userName!=null && !userName.equals(""))
??{
?????ResultSet rst = connBean.ExecuteQuery("SELECT Email FROM Classmates WHERE UserName = '"+userName+"' ");
?????if (rst.next())
??????from = rst.getString("Email");????? //發件人地址
?????else from = "[email]zyf0808@yeah.net[/email]";? //換成可用的管理員的郵箱地址
??}
?else
??{
???//未登錄
%>
<SCRIPT language="JavaScript">
?? alert("對不起,您尚未登錄,只有登錄的管理員同學才能使用該功能!")
?? window.location.replace("../login.jsp");
</SCRIPT>
<%?
??}
?//得到除當前登錄管理員外的所有用戶的郵箱
?rs = connBean.ExecuteQuery("SELECT Email FROM Classmates WHERE UserName <> '"+userName+"'");
??? while (rs.next())
?{
??to = rs.getString("Email"); // 接受人郵件地址
?? if (to !=null && !to.equals("") )
??{
??try
??{
???Properties props = new Properties();
???props.put("mail.smtp.host", smtphost);
???props.put("mail.smtp.auth","true");
???Session ssn = Session.getInstance(props, null); MimeMessage message = new MimeMessage(ssn); InternetAddress fromAddress = new InternetAddress(from);
???message.setFrom(fromAddress);
???InternetAddress toAddress = new InternetAddress(to);
???message.addRecipient(Message.RecipientType.TO, toAddress); message.setSubject(subject);
???message.setText(body); Transport transport = ssn.getTransport("smtp");
???transport.connect(smtphost, user, password);
???transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
???transport.close();
??}
??catch(Exception e)
???{
????out.println(e.toString());
???}
??}
?}
%>
<SCRIPT language="JavaScript">
?? alert("郵件已經成功發送!")
?? window.location.replace("../index.jsp");
</SCRIPT>
</BODY>
</HTML>
總結
以上是生活随笔為你收集整理的jsp中jsp中群发邮件群发邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataGrid Web Control
- 下一篇: u盘里有文件夹清理不了怎么办 如何清理无