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 on the JavaMail API, you can download this site, and set the classpath according to Readme.txt) 1. JavaMail some of the class 1.properties JavaMail needs Properties to create a session object, its property value is sending an email Host, 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. All JavaMail-based programs require at least one or all of the dialogue targets.
Session session = session.getInstance (props, null); 3. MimeMessage information object will reflect the true reflection of the email you sent.
MimeMessage Msg = New MimeMessage (session); 4.Transport mail is sent by Transport:
TRANSPORT.SEND (MSG); Second, we create a class of accessories that can be sent
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 servletimport 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 {// Institutes the class we did, and in vicinted the corresponding parameters by its constructs Mail sendmail = newmail ("zhang@263.net", "smtp.21cn.com", "SMTP.21CN.COM", " Test "); sendmail.attachfile (" Table.pdf "); sendmail.startsend ();} catch (exception e) {E.PrintStackTrace ();}} public void design;}}} four, narrow here, you It is already possible to add an attachment in the message and 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".
Author Blog:
http://blog.9cbs.net/fxfboy/