Recently encountered a problem at work:
Customers need to have our system send them email, but don't give us a user password.
This leads to a problem, that is, generally we use the SMTP way to send e-mail, e-mail must be logged in, although you can modify the sender's mailbox name. But still need to login AH.
On this question, I began to explore. Search all the major domestic websites have no examples.
Here is the code that I have here to solve the problem:
1 Public Static voidSend () {2 //Recipient ' s email ID needs to be mentioned.3String to = "[Email protected]";4 5 //Sender ' s email ID needs to be mentioned6String from = "[Email protected]";7 8 //assuming you is sending email from localhost9String host = "xxx.xxx.36.3";Ten One //Get System Properties AProperties Properties =system.getproperties (); - - //Setup Mail Server theProperties.setproperty ("Mail.smtp.host", host); - - //Get The default Session object. -Session session =Session.getdefaultinstance (properties); + - Try{ + //Create a default MimeMessage object. AMimeMessage message =NewMimeMessage (session); at - //Set from:header field of the header. -Message.setfrom (Newinternetaddress (from)); - - //Set to:header field of the header. - message.addrecipient (Message.RecipientType.TO, in Newinternetaddress (to)); - to //Set subject:header Field +Message.setsubject ("This is the Subject line!"); - the //Now set the actual message *Message.settext ("This is actual message"); $ Panax Notoginseng //Send Message - transport.send (message); theSYSTEM.OUT.PRINTLN ("Sent message successfully ..."); +}Catch(Messagingexception Mex) { A mex.printstacktrace (); the } +}
Using this method can be achieved, do not log in to send anonymous mail, the perfect solution to my problem, I hope to yimeimei helpful.
Thank you ~
Java do not log in to send mail noreply