The JavaMail API provided by Sun can easily develop mail senders. Maybe you can use it to send a simple text, but I don't want to make your program like Outlook to send attachments? This article briefly introduces Javamail, explained in detail a complete JavaBean and a very light servlet.
(There is no reader loaded by JavaMail API, you can
This site downloads and sets the ClassPath according to Readme.txt.
I. Some of the class we need in Javamail
Properties
JavaMail needs Properties to create a session object, its property value is the host that sends an email, such as:
Properties PROPS = New Properties (); Props.Put ("mail.xxx.com"); // You can replace your SMTP host name, just like you set SMTP host name in Outlook same.
2.Session
All JavaMail-based programs require at least one or all of the dialogue targets.
Session session = session.getInstance (Props, NULL);
3.mimeMessage
The information object will reflect the email you sent true.
MIMEMESSAGE MSG = New MimeMessage (session);
4.Transport
The email is delivered by Transport:
Transport.send (MSG);
Second, the class that we create can send accessories
Import java.util. *; import java.io. *; import javax.mail. *; import javax.mail.internet. *; import javax.activation. *; public class mail {// Define sender, recipient People, themes, etc. String to = ""; string from = ""; string host = ""; string filename = ""; string subject = "; // for saving the file name of the file name of the accessory, vector file = New Vector (); // Do a construction of the parameters such as a member of public mail (string to, string from, string smtpserver, string subject) {// initializer, recipient, theme, etc. THISTO = THIS.FROM = from; this.host = SMTPSERVER; this.subject = Subject;} // This method is used to collect attachment name public void attachfile (String File.Addelement (fname);} // Start sending Method PUBLIC Boolean Startsend () {// Create a Properties Object Properties Props = System.getProperties (); // Create a Letter Server Props.Put ("mail.smtp.host", host); // Get the default conversation object Session session = session.GETDEFAULTINSTANCE (PROPS, NULL); Try {// Create a message and initialize the elements of the message mimeMessage MSG = new mimeMessage (session); Msg.SetFrom (New InternetdRESS (FROM)); InternetAddress [ ] address = {new internetdress (to)}; msg.seTrecipients (message.recipienttype.to, address); msg.setSubject (Subject); // The back Bodypart will join into the multipart created by Multipa RT mp = new mimemultipart (); // Use the enumerator convenient traversal collection ENUMERATION EFILE = file.elements (); // Check if there is more object while in the sequence (Efile.hasMoreElements ()) {MIMEBODYPART MBP = new mimebodypart (); // Select each attachment name FileName = Efile .NexTelement (). TOSTRING (); // Get data source fileDataSource fds = new fileDataSource (filename); // Get the attachment itself and enters BodypartMBP. SetDataHandler (New DataHandler (FDS)); // Get the file name is the same as BodypartMbp.setFileName (fds.getname ()); mp.addbodypart (MBP);} // remove all Elements in the collection File.RemoveAlleferences () ; // Multipart Add to Letter Msg.setContent (MP); // Set the send date msg.setsentdate (new date ()) (new date ()) (new Date ()); // Send Letters Transport.send (} catch (messagingException mex) { MEX.PrintStackTrace (); Exception EX = NULL;
IF ((EX = Mex.getNextexception ())! = null) {EX.PrintStackTrace ();} return false;} Return true;}} 3, a simple servlet
import javax.servlet *;. import javax.servlet.http *;. import java.io *;. public class SendMail extendsHttpServlet implements SingleThreadModel {public void init (ServletConfig Conf) throws ServletException {super.init (Conf);} public void doPost (HttpServletRequestReq, HttpServletResponse Res) throws ServletException, IOException {try {// do we just instantiated class configured in accordance pass into corresponding parameter Mail sendmail = newMail ( "zhang@263.net", "chtwoy @ 21cn .com "," smtp.21cn.com "," test "); sendmail.attachfile (" Table.pdf "); sendmail.startsend ();} catch (exception e) {E.PrintStackTrace ();}} PUBLIC Void destroy () {}}
Fourth, small knot
At this point, you can already add an attachment to the email, and you can be multiple. Like this, it is quite good to expand and maintain the response and logic phase separation. If Beans is not in the same package, don't forget "import".