There should be more than one way for Java to send mail
All I'm saying here is that I will.
First add two jar packages Mail.jar and Activation.jar
Then write yourself a mail class that includes methods such as sending
1 Public classMail {2 PrivateString subject;3 PrivateString from;4 Privatestring[] recipients;5 PrivateString message;6 7 Private Static FinalString smtp_host_name = "";//your SMTP server address8 Private Static FinalString smtp_auth_user = "";//SMTP User name9 Private Static FinalString smtp_auth_pwd = "";//password;Ten One Public voidSend ()throwsmessagingexception{ A Booleandebug =false; - -Properties props =NewProperties (); theProps.put ("Mail.smtp.host", smtp_host_name); -Props.put ("Mail.smtp.auth", "true"); - -Session session = Session.getdefaultinstance (props,Newsmtpauthenticator ()); + session.setdebug (debug); - + //Create a message AMessage msg =NewMimeMessage (session); atMsg.setfrom (Newinternetaddress (from)); - -internetaddress[] Addressto =NewInternetaddress[recipients.length]; - for(inti = 0; i < recipients.length; i++) - { -Addressto[i] =Newinternetaddress (Recipients[i]); in } - msg.setrecipients (Message.RecipientType.TO, addressto); to +Msg.setheader ("X-priority", "3");//1: Emergency 3: normal 5: Slow - Msg.setsubject (subject); theMsg.setcontent (Message, "Text/plain; Charset=utf-8 "); * transport.send (msg); $ }Panax Notoginseng - //to certify the sender, open the following comment the Private classSmtpauthenticatorextendsJavax.mail.Authenticator + { A the Publicpasswordauthentication getpasswordauthentication () + { -String username =Smtp_auth_user; $String Password =smtp_auth_pwd; $ return Newpasswordauthentication (username, password); - } - } the - PublicString Getsubject () {Wuyi returnsubject; the } - Wu - Public voidSetsubject (String subject) { About This. Subject =subject; $ } - - - PublicString Getfrom () { A returnfrom ; + } the - $ Public voidSetfrom (String from) { the This. from =from ; the } the the - Publicstring[] Getrecipients () { in returnrecipients; the } the About the Public voidsetrecipients (string[] recipients) { the This. Recipients =recipients; the } + - the PublicString getMessage () {Bayi returnmessage; the } the - - Public voidsetmessage (String message) { the This. Message =message; the } the the - the}View Code
Such references in other classes are possible:
Private Static voidsendrenew (String tomail,string message) {Mail mail=NewMail (); Mail.setfrom ("License Manager <[email protected]>");//Sending PartyMail.setrecipients (NewString[]{tomail});//receiver, you can have multipleMail.setsubject ("Renew now!!"); Mail.setmessage (message); Try{mail.send ();//Send}Catch(Exception e) {System.out.println ("Error report failure.\n" +e); } }View Code
That's fine, but if you're using myeclipse, sometimes this happens.
Java.lang.noclassdeffounderror:com/sun/mail/util/lineinputstream
Workaround:
Enter the installation directory of the MyEclipse
D:\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\ee_5
(The blue part differs slightly in different versions )
Can see the Javaee.jar package,
Use the compression software to open the package file, then go to the Javax folder, delete the mail directory and the activation folder
( that is, the MyEclipse also comes with such a feature pack, except that the method is inconsistent with the JDK's JavaMail package, which results in a report exception )
After you delete the code, you will not get an error when you run it.
Reference: http://blog.sina.com.cn/s/blog_4550f3ca01019qpt.html, thanks for the Big Brother.
Java Send mail