Another new project was launched. Although it is still in the system design stage, from the perspective of the current system requirements, one of the functions is to send system emails through the Web. Make preparations in advance. Well, it took an afternoon to encapsulate a mailservice class and call it directly. This article only describes how to use javamail to send emails, including SMTP authentication, email attachment sending, and HTML content emails.
System Environment: Windows XP SP3
Development Environment: Eclipse 3.5/JDK 1.6/struts2/tiles
Test Tool: Firefox 3.6/IE 8
Test mailbox: Netease mailbox, IBM Lotus Notes mailbox
Jar package: mail. Jar (including mailservice. java files)
Mailservice. Java file (encapsulated mail class ):
- Package com. Olympus. util;
-
- Import java. util. date;
- Import java. util. properties;
-
- Import javax. Activation. datahandler;
- Import javax. Activation. filedatasource;
- Import javax. Mail. authenticator;
- Import javax. Mail. multipart;
- Import javax. Mail. passwordauthentication;
- Import javax. Mail. Session;
- Import javax. Mail. Transport;
- Import javax. Mail. Internet. internetaddress;
- Import javax. Mail. Internet. mimebodypart;
- Import javax. Mail. Internet. mimemessage;
- Import javax. Mail. Internet. mimemultipart;
- Import javax. Mail. Internet. mimeutility;
-
- Public class mailservice extends authenticator {
- Private string m_username = NULL;
- Private string m_userpass = NULL;
- Public void setusername (string username ){
- M_username = username;
- }
- Public void setuserpass (string userpass ){
- M_userpass = userpass;
- }
- Public mailservice (string username, string userpass ){
- Super ();
- Setusername (username );
- Setuserpass (userpass );
- }
- Public passwordauthentication getpasswordauthentication (){
- Return new passwordauthentication (m_username, m_userpass );
- }
- Public static void Sendmail (
- String subject, string from,
- String [] to, string text,
- String [] filenames, string mimetype) throws exception
- {
- Properties props = new properties ();
- String SMTP = "smtp.163.com"; // you can specify the SMTP server address used to send emails.
- String smtpname = "username"; // you can specify the username used to log on to the SMTP server.
- String smtppassword = "password"; // set the password for logging on to the SMTP server
- Session mailsession; // email Session Object
- Mimemessage mimemsg; // mime email object
- Props = java. Lang. system. getproperties (); // obtain the system property object
- Props. Put ("mail. SMTP. Host", SMTP); // set the SMTP host
- Props. Put ("mail. SMTP. Auth", "true"); // do I need to verify the user name and password to link to the SMTP server?
- // Verify that the user name and password sent are correct on the server
- Mailservice myemailauther = new mailservice (smtpname, smtppassword );
- // Set the email session
- Mailsession = session. getinstance (props, (authenticator) myemailauther );
- Transport transport = mailsession. gettransport ("SMTP"); // you can specify the transmission protocol.
- Mimemsg = new mimemessage (mailsession); // sets information such as from and.
- Internetaddress sentfrom = new internetaddress (from );
- Mimemsg. setfrom (sentfrom); // sets the sender address.
- Internetaddress [] sendto = new internetaddress [to. Length];
- For (INT I = 0; I <to. length; I ++ ){
- // System. Out. println ("sent to:" + to [I]);
- Sendto [I] = new internetaddress (to [I]);
- }
- Mimemsg. setrecipients (mimemessage. recipienttype. To, sendto );
- Mimemsg. setsubject (subject, "UTF-8 ");
- Mimebodypart messagebodypart1 = new mimebodypart ();
- // Messagebodypart. settext (unicodetochinese (text ));
- Messagebodypart1.setcontent (text, mimetype );
- Multipart = new mimemultipart (); // The attachment transmission format.
- Multipart. addbodypart (messagebodypart1 );
- For (INT I = 0; I <filenames. length; I ++ ){
- Mimebodypart messagebodypart2 = new mimebodypart ();
- String filename = filenames [I]. Split (",") [0]; // select the name of each attachment.
- String displayname = filenames [I]. Split (",") [1];
- Filedatasource FDS = new filedatasource (filename); // obtain the data source
- // Obtain the attachment and add it to the bodypart
- Messagebodypart2.setdatahandler (New datahandler (FDS ));
- // Get the same name as bodypart
- // Messagebodypart2.setfilename (displayname );
- // Messagebodypart2.setfilename (FDS. getname ());
- Messagebodypart2.setfilename (mimeutility. encodetext (displayname ));
- Multipart. addbodypart (messagebodypart2 );
- }
- Mimemsg. setcontent (multipart );
- Mimemsg. setsentdate (new date (); // you can specify the sending date of the mail header.
- Mimemsg. savechanges ();
- Transport. Send (mimemsg); // send an email
- Transport. Close ();
- }
- }
Call in action
- @ Action (value = "//",
- Results = {@ result (name = "success", location = "Index", type = "tiles ")})
- Public String index (){
- String sendto [] = {"liang.xu@ohc.olympus.com.hk "};
- String filenames [] = {};
- Try {
- Mailservice. Sendmail (
- "Javaweb send mail test", "Alex Xu <xht555@163.com> ",
- Sendto, "Java Web mail sending test ",
- Filenames, "text/html; charsets = UTF-8 ");
- } Catch (exception e ){
- E. printstacktrace ();
- }
- Return success;
- }
- Test Results
- My Netease mailbox shows that the email is successfully sent: ② my work mailbox (IBM Lotus Notes) Successfully received the email from Netease mail:
: