SendMailServlet2.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:6k
源码类别:
Java编程
开发平台:
Java
- package bible.javamail;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.util.*;
- import javax.activation.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.naming.*;
- /**
- * Class SendMailServlet2
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class SendMailServlet2 extends HttpServlet {
- private String to = "";
- private String cc = "";
- private String bcc = "";
- private String subject = "";
- private String filename = "";
- private String messageText = "";
- private boolean isHTML;
- private String successMessage = "";
- /**
- * Method doGet
- *
- *
- * @param req
- * @param res
- *
- * @throws IOException
- * @throws ServletException
- *
- */
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- PrintWriter pw = res.getWriter();
- res.setContentType("text/html");
- pw.println("<HTML><HEAD><TITLE>Send Mail - Complex</TITLE></HEAD>");
- pw.println("<BODY><H1>Send Mail - Complex</H1>");
- pw.println("<FORM ACTION='sendmail2' METHOD=post><TABLE>");
- pw.println("<TR><TD>To:</TD><TD><INPUT NAME=to size=60></TD></TR>");
- pw.println("<TR><TD>Cc:</TD><TD><INPUT NAME=cc size=60></TD></TR>");
- pw.println("<TR><TD>Bcc:</TD><TD><INPUT NAME=bcc size=60></TD></TR>");
- pw.println("<TR><TD>Subject:</TD><TD><INPUT NAME=subject size=60>"
- + "</TD></TR>");
- pw.println("<TR><TD>Enclosure:</TD><TD>"
- + "<INPUT NAME=filename size=60></TD></TR>");
- pw.println("<TR><TD VALIGN>Message:</TD>");
- pw.println("<TD><TEXTAREA NAME=messageText rows=10 cols=60>"
- + "</TEXTAREA></TD></TR>");
- pw.println("<TR><TD>HTML:</TD><TD><input type=checkbox NAME=isHTML>"
- + "</TD></TR>");
- pw.println("<TR><TD><INPUT TYPE=SUBMIT NAME=Submit VALUE=Submit>"
- + "</TD></TR>");
- pw.println("</TABLE></FORM>");
- pw.println("<STRONG>" + successMessage + "</STRONG>");
- pw.println("</BODY></HTML>");
- }
- /**
- * Method doPost
- *
- *
- * @param req
- * @param res
- *
- * @throws IOException
- * @throws ServletException
- *
- */
- public void doPost(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- to = req.getParameter("to");
- cc = req.getParameter("cc");
- bcc = req.getParameter("bcc");
- subject = req.getParameter("subject");
- filename = req.getParameter("filename");
- messageText = req.getParameter("messageText");
- isHTML = "on".equals(req.getParameter("isHTML"));
- sendMessage();
- doGet(req, res);
- }
- /**
- * Method sendMessage
- *
- *
- */
- public void sendMessage() {
- try {
- // lookup mail session in JNDI
- InitialContext context = new InitialContext();
- Session session = (Session) context.lookup("BibleMailSession");
- // override mail session properties
- Properties mailProps = new Properties();
- mailProps.put("mail.transport.protocol", "smtp");
- mailProps.put("mail.host", "xxx.xxx.xxx.xxx");
- mailProps.put("mail.user", "username");
- mailProps.put("mail.from", "username@company.com");
- mailProps.put("mail.debug", "true");
- mailProps.put("mail.smtp.auth", "true");
- // get an authenticated session
- MailAuthenticator mailAuth = new MailAuthenticator();
- // For testing, set the values here. The user should be prompted or
- // retrieved from a database or descriptor
- mailAuth.setUser("username");
- mailAuth.setPassword("password");
- Session authSession = session.getInstance(mailProps, mailAuth);
- // set message with to, subject, and message text
- Message msg = new MimeMessage(authSession);
- // set message with to, cc, bcc, and subject
- msg.setFrom();
- msg.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(to, false));
- msg.setRecipients(Message.RecipientType.CC,
- InternetAddress.parse(cc, false));
- msg.setRecipients(Message.RecipientType.BCC,
- InternetAddress.parse(bcc, false));
- msg.setSubject(subject);
- msg.setSentDate(new Date());
- // set with message text via a mime multipart
- Multipart mp = new MimeMultipart();
- MimeBodyPart mbp1 = new MimeBodyPart();
- if (isHTML) {
- mbp1.setContent(messageText, "text/html");
- } else {
- mbp1.setText(messageText);
- }
- mp.addBodyPart(mbp1);
- if (!filename.equals("")) {
- MimeBodyPart mbp2 = new MimeBodyPart();
- FileDataSource fds = new FileDataSource(filename);
- mbp2.setDataHandler(new DataHandler(fds));
- mbp2.setFileName(fds.getName());
- mp.addBodyPart(mbp2);
- }
- msg.setContent(mp);
- msg.saveChanges();
- // get transport and send message
- Transport authTransport = authSession.getTransport();
- authTransport.connect();
- authTransport.sendMessage(msg, msg.getAllRecipients());
- authTransport.close();
- successMessage = "Message sucessfully sent.";
- session = null;
- } catch (Exception e) {
- successMessage = e.getMessage();
- }
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/