Java send email (with proxy mode, SSL, traditional way)

Source: Internet
Author: User

 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)

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.