Java mail sending instance

Source: Internet
Author: User

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 ):

  1. Package com. Olympus. util;
  2.  
  3. Import java. util. date;
  4. Import java. util. properties;
  5.  
  6. Import javax. Activation. datahandler;
  7. Import javax. Activation. filedatasource;
  8. Import javax. Mail. authenticator;
  9. Import javax. Mail. multipart;
  10. Import javax. Mail. passwordauthentication;
  11. Import javax. Mail. Session;
  12. Import javax. Mail. Transport;
  13. Import javax. Mail. Internet. internetaddress;
  14. Import javax. Mail. Internet. mimebodypart;
  15. Import javax. Mail. Internet. mimemessage;
  16. Import javax. Mail. Internet. mimemultipart;
  17. Import javax. Mail. Internet. mimeutility;
  18.  
  19. Public class mailservice extends authenticator {
  20. Private string m_username = NULL;
  21. Private string m_userpass = NULL;
  22. Public void setusername (string username ){
  23. M_username = username;
  24. }
  25. Public void setuserpass (string userpass ){
  26. M_userpass = userpass;
  27. }
  28. Public mailservice (string username, string userpass ){
  29. Super ();
  30. Setusername (username );
  31. Setuserpass (userpass );
  32. }
  33. Public passwordauthentication getpasswordauthentication (){
  34. Return new passwordauthentication (m_username, m_userpass );
  35. }
  36. Public static void Sendmail (
  37. String subject, string from,
  38. String [] to, string text,
  39. String [] filenames, string mimetype) throws exception
  40. {
  41. Properties props = new properties ();
  42. String SMTP = "smtp.163.com"; // you can specify the SMTP server address used to send emails.
  43. String smtpname = "username"; // you can specify the username used to log on to the SMTP server.
  44. String smtppassword = "password"; // set the password for logging on to the SMTP server
  45. Session mailsession; // email Session Object
  46. Mimemessage mimemsg; // mime email object
  47. Props = java. Lang. system. getproperties (); // obtain the system property object
  48. Props. Put ("mail. SMTP. Host", SMTP); // set the SMTP host
  49. Props. Put ("mail. SMTP. Auth", "true"); // do I need to verify the user name and password to link to the SMTP server?
  50. // Verify that the user name and password sent are correct on the server
  51. Mailservice myemailauther = new mailservice (smtpname, smtppassword );
  52. // Set the email session
  53. Mailsession = session. getinstance (props, (authenticator) myemailauther );
  54. Transport transport = mailsession. gettransport ("SMTP"); // you can specify the transmission protocol.
  55. Mimemsg = new mimemessage (mailsession); // sets information such as from and.
  56. Internetaddress sentfrom = new internetaddress (from );
  57. Mimemsg. setfrom (sentfrom); // sets the sender address.
  58. Internetaddress [] sendto = new internetaddress [to. Length];
  59. For (INT I = 0; I <to. length; I ++ ){
  60. // System. Out. println ("sent to:" + to [I]);
  61. Sendto [I] = new internetaddress (to [I]);
  62. }
  63. Mimemsg. setrecipients (mimemessage. recipienttype. To, sendto );
  64. Mimemsg. setsubject (subject, "UTF-8 ");
  65. Mimebodypart messagebodypart1 = new mimebodypart ();
  66. // Messagebodypart. settext (unicodetochinese (text ));
  67. Messagebodypart1.setcontent (text, mimetype );
  68. Multipart = new mimemultipart (); // The attachment transmission format.
  69. Multipart. addbodypart (messagebodypart1 );
  70. For (INT I = 0; I <filenames. length; I ++ ){
  71. Mimebodypart messagebodypart2 = new mimebodypart ();
  72. String filename = filenames [I]. Split (",") [0]; // select the name of each attachment.
  73. String displayname = filenames [I]. Split (",") [1];
  74. Filedatasource FDS = new filedatasource (filename); // obtain the data source
  75. // Obtain the attachment and add it to the bodypart
  76. Messagebodypart2.setdatahandler (New datahandler (FDS ));
  77. // Get the same name as bodypart
  78. // Messagebodypart2.setfilename (displayname );
  79. // Messagebodypart2.setfilename (FDS. getname ());
  80. Messagebodypart2.setfilename (mimeutility. encodetext (displayname ));
  81. Multipart. addbodypart (messagebodypart2 );
  82. }
  83. Mimemsg. setcontent (multipart );
  84. Mimemsg. setsentdate (new date (); // you can specify the sending date of the mail header.
  85. Mimemsg. savechanges ();
  86. Transport. Send (mimemsg); // send an email
  87. Transport. Close ();
  88. }
  89. }

Call in action

  1. @ Action (value = "//",
  2. Results = {@ result (name = "success", location = "Index", type = "tiles ")})
  3. Public String index (){
  4. String sendto [] = {"liang.xu@ohc.olympus.com.hk "};
  5. String filenames [] = {};
  6. Try {
  7. Mailservice. Sendmail (
  8. "Javaweb send mail test", "Alex Xu <xht555@163.com> ",
  9. Sendto, "Java Web mail sending test ",
  10. Filenames, "text/html; charsets = UTF-8 ");
  11. } Catch (exception e ){
  12. E. printstacktrace ();
  13. }
  14. Return success;
  15. }
  16. Test Results
  17. My Netease mailbox shows that the email is successfully sent: ② my work mailbox (IBM Lotus Notes) Successfully received the email from Netease mail:


  18. :

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.