Send email learning notes using JSP
Here you can download javamail package http://java.sun.com/products/javamail/
After decompression, copy Activation.jar and Mail.jar to / web-inf / lib directory and restart the server, JavaMail can be used normally.
The following JavaMail.jsp content is as follows
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.util. *, javax.mail. *, javax.mail.internet. *"%> < Head>
<% // The following variables set string smtphost = "SMTP.NOW.NET.NET.CN" according to their own situation; // Send a mail server string user = "yauboo"; // mail server login user name String password = "xxx "; // mail server login password string from =" yauboo@now.net.cn "; // sender email address string to =" yauboo@now.net.cn "; // Accept people email address string subject =" Java Mail Subject! "; // Email Title String Body =" Java Mail Body! "; // Mail content
// The following is a sender, the user does not need to change try {Properties PROPS = New Properties (); "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.send (message); Transport.close ();%>
Your email has been sent, please return. p> <%} catCH (Exception M) {Out.println (m.toString ());}%> body> html>