td>
TR>
TABLE>
form>
body>
html>
Then, we write a handler Testmail.jsp, as follows:
-------------------------------------------------- --------------------------------------- <% @ Page ContentType = "Text / HTML; Charset = GB2312 "%>
<% Request.SetCharacterencoding ("GB2312");%>
<% @ Page Import = "java.util. *, javax.mail. *"%>
<% @ Page Import = "javax.mail.internet. *"%>
Send Success Title>
hEAD>
<%
Try {
// Get mail information from the HTML form
String TTO = Request.getParameter ("to");
String Ttitle = Request.GetParameter ("Title");
String tcontent = Request.getParameter ("Content");
Properties PROPS = New Properties (); // You can also use Properties PROPS = System.getProperties ();
Props.Put ("mail.smtp.host", "smtp.163.net"); // Store information sent to the mail server
Props.put ("mail.smtp.auth", "true"); // simultaneously verified
Session s = session.getInstance (PrOPS); // Newly built a message session according to attribute
S.SETDEBUG (TRUE);
MimeMessage Message = New MimeMessage (s); // Newly built a message object by the mail session
// Set the mail
InternetAddress from = new internetdress ("boy@163.net");
Message.SetFrom (from); // Set the sender
InternetAddress to = New InternetAddress (TTO);
Message.seTrecipient (Message.RecipientType.to, To); // Set the recipient, and set its reception type to To
Message.setSubject (Ttitle); // Set the topic
Message.setText (tcontent); // Set the content content
Message.setSentDate (new date ()); // Set the send time
//send email
Message.savechanges (); // Store mail information
Transport Transport = S.GetTransport ("SMTP");
Transport.connect ("SMTP.163.NET", "BOY", "IloveYou"); // Log in to the mailbox in SMTP
Transport.sendMessage (Message, Message.GetallRecipients ()); // Send a message, where the second parameter is all // has set the recipient address
Transport.close ();
%>
Send success! font> p>
Go see my mailbox
Send a sealed p>
div>
<%
} catch (messagingException e) {
Out.println (E.TOString ());
}
%>
body>
html>
**********************************note*************** ***********************
There are many books and online articles to write TestMail.jsp in key parts, as follows:
String TTO = Request.getParameter ("to");
String Ttitle = Request.GetParameter ("Title");
String tcontent = Request.getParameter ("Content");
Properties PROPS = New Properties ();
Props.Put ("Mail.smtp.host", "SMTP.163.NET");
Session s = session.getInstance (PROPS);
MimeMessage Message = New MimeMessage (s);
InternetAddress from = new internetdress ("boy@163.net");
Message.SetFrom (from);
InternetAddress to = New InternetAddress (TTO);
Message.setRecipient (Message.RecipientType.to, To);
Message.setSubject (TTITLE);
Message.Settext (tcontent);
Message.setSentDate (New Date ());
Store = S.GetStore ("POP3");
Store.Connect ("POP.163.NET", "BOY", "IloveYou"); // Log in to the mailbox in POP3
Transport Transport = S.GetTransport ("SMTP");
TRANSPORT.SEND (Message);
Store.Close ();
In fact, this approach is not reliable, because many of the electronic post office's SMTP server requires us to verify, so when you send an email in this way, you can only send it to the same type of mailbox (ie the same SMTP mailbox), and even sometimes the same type of mailbox also I can't make it. I have tried many ways above, and the results prove that the first way is the most reliable.
Ok, I believe you should write the simplest email sender .ok, the next time we will talk about how to write an email to send HTML format.
(to be continued)
转载请注明原文地址:https://www.9cbs.com/read-24744.html
|