1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.ParameterParser;
import com.oreilly.servlet.ServletUtils;
import sun.net.smtp.SmtpClient;
public class EmailServlet extends HttpServlet {
//private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
private final static String DEFAULT_SERVER = "mail.attbi.com";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/**Process the HTTP doGet request.
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**Process the HTTP doPost request.
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String smtpServ = DEFAULT_SERVER;
String from = "java2s@yourserver.com";
String to = "yourname@yourserver.com";
String subject = "subject line";
String emailContent = "emailContent";
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out
.println("<html><head><title>Email message sender</title></head><body>");
try {
sendMessage(smtpServ, to, from, subject, emailContent);
} catch (Exception e) {
throw new ServletException(e.getMessage());
}
out.println("<h2>The message was sent successfully</h2>");
out.println("</body></html>");
}
private void sendMessage(String smtpServer, String to, String from,
String subject, String emailContent) throws Exception {
Properties properties = System.getProperties();
//populate the 'Properties' object with the mail
//server address, so that the default 'Session'
//instance can use it.
properties.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(properties);
Message mailMsg = new MimeMessage(session);//a new email message
InternetAddress[] addresses = null;
try {
if (to != null) {
//throws 'AddressException' if the 'to' email address
//violates RFC822 syntax
addresses = InternetAddress.parse(to, false);
mailMsg.setRecipients(Message.RecipientType.TO, addresses);
} else {
throw new MessagingException(
"The mail message requires a 'To' address.");
}
if (from != null) {
mailMsg.setFrom(new InternetAddress(from));
} else {
throw new MessagingException(
"The mail message requires a valid 'From' address.");
}
if (subject != null)
mailMsg.setSubject(subject);
if (emailContent != null)
mailMsg.setText(emailContent);
//Finally, send the meail message; throws a 'SendFailedException'
//if any of the message's recipients have an invalid adress
Transport.send(mailMsg);
} catch (Exception exc) {
throw exc;
}
}
} |
Partager