Packagespring.vhostall.com;Importjava.security.Security;Importjava.util.Date;Importjava.util.Properties;Importjavax.mail.Address;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage; Public classMailTest {/*** Send mail via agent*/ /** * @paramargs*/ Public Static voidMain (string[] args)throwsException {mailtest test=NewMailTest (); //Send mail//Test.sendmail (); //Send mail via proxyTest.sendmailbyproxy (); } Private voidSendmailbyproxy ()throwsexception{Security.addprovider (NewCom.sun.net.ssl.internal.ssl.Provider ()); //setting up a proxy serverProperties props =system.getproperties (); Props.setproperty ("Proxyset", "true"); Props.setproperty ("Socksproxyhost", "43.82.112.214"); Props.setproperty ("Socksproxyport", "1080"); Props.setproperty ("Mail.smtp.host", "smtp.163.com"); Props.setproperty ("Mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory"); Props.setproperty ("Mail.smtp.socketFactory.fallback", "false"); Props.setproperty ("Mail.smtp.port", "465"); Props.setproperty ("Mail.smtp.socketFactory.port", "465"); Props.setproperty ("Mail.smtp.auth", "true"); Props.setproperty ("Mail.debug", "true"); Props.setproperty ("Mail.transport.protocol", "SMTP"); FinalString username = "* * *"; FinalString Password = "* * *"; //Use validationSession session =session.getdefaultinstance (Props,NewJavax.mail.Authenticator () {protectedpasswordauthentication getpasswordauthentication () {return Newjavax.mail.PasswordAuthentication (Username,password); } }); MimeMessage message=NewMimeMessage (session); Address Address=NewInternetAddress ("* * * *"); Address toaaddress=NewInternetAddress ("* * * *"); Message.setfrom (address); Message.setrecipient (MimeMessage.RecipientType.TO, toaaddress); Message.setsubject (Test); Message.settext ("Test"); Message.setsentdate (NewDate ()); Transport Transport=Session.gettransport (); Transport.connect ("Smtp.163.com", "* * *", "* * *"); Transport.sendmessage (Message,NewAddress[] {NewInternetAddress ("* * * *")});//transport.send (message);System.out.println ("Mail Send! "); Transport.close (); } Public voidSendMail () {/* try {String host = "smtp.gmail.com"; String from = "[email protected]"; String to = "@163.com"; String username = "* * *"; String Password = "* * *"; String BODY = "This is a test"; String subject = "Email Test"; Smtpauth sa = new Smtpauth (); Sa.getuserinfo (username, password); Session session; MimeMessage message; Properties props = System.getproperties (); Props.setproperty ("Proxyset", "true"); Props.setproperty ("Socksproxyhost", "43.82.218.50"); Props.setproperty ("Socksproxyport", "8080"); Props.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", host); Session = session.getinstance (props, SA); Session.setdebug (TRUE); message = new MimeMessage (session); Message.setfrom (New InternetAddress (from)); Message.setrecipient (Message.RecipientType.TO, New internetaddress (to)); Message.setsubject (subject); MimeBodyPart MBP = new MimeBodyPart (); Mbp.settext (body); Multipart MP = new Mimemultipart (); Mp.addbodypart (MBP); MEssage.setcontent (MP); Message.setsentdate (New Java.util.Date ()); Transport Transport = Session.gettransport ("SMTP"); Transport.connect (host, username, password); Transport.send (message); } catch (Messagingexception e) {System.out.println ("error" + e.getmessage ()); }*/} classSmtpauthextendsJavax.mail.Authenticator {PrivateString user, password; Public voidGetUserInfo (String getuser, string getpassword) {User=GetUser; Password=GetPassword; } protectedjavax.mail.PasswordAuthentication getpasswordauthentication () {return Newjavax.mail.PasswordAuthentication (user, password); } }}
Dependent jar Package: http://download.csdn.net/detail/wu858773457/8699111
Java send email (with proxy mode, SSL, traditional way)