Java Send mail

Source: Internet
Author: User

Import Java.util.properties;import Java.util.regex.matcher;import java.util.regex.pattern;import Javax.mail.address;import Javax.mail.message;import Javax.mail.session;import Javax.mail.Transport;import Javax.mail.internet.internetaddress;import Javax.mail.internet.MimeMessage;  Public class SendEmail {    /**      *

      

String uto= "[email protected]";//Get recipient Mailbox
String topic= "System error";//Send subject
String content= "error";//Send Content
String sendemail= "[email protected]";
String sendname= "Terminator System";
String connecthost= "smtp.163.com";
String connectemailname= "sys857";
String connectemailpass= "Cyunlnvuctkqwtvs";

* @param uto Get recipient mailbox * @param topic Send subject * @param content sent * @param sendemail sender's Email * @para M sendname Sender Name * @param connecthost Mailbox Host * @param connectemailname mailbox User name * @param connectemailpass Mailbox Send key (not login password) * @return Boolean*/         Public Staticboolean SendEmail (String uto,string topic,string content, string sendemail,string sendname,string connecth OST, String connectemailname,string Connectemailpass,integer port) {//Verify that the mailbox format startsBoolean flag=true; String Str="^ ([a-z0-9a-z]+[-|_|\\]?) +[a-z0-9a-z]@ ([a-z0-9a-z]+ (-[a-z0-9a-z]+) \ \.) +[a-za-z]{2,}$"; Pattern Regex=pattern.compile (str); Matcher Matcher=Regex.matcher (UTO); Flag=matcher.matches (); //Mailbox Verification                Try {        if(flag) {//set up ProtocolsProperties Pros =NewProperties (); Pros.put ("Mail.transport.protocol","SMTP"); Pros.put ("Mail.smtp.auth",true); Pros.put ("Mail.smtp.port", port);//QQPros.setproperty ("Mail.debug","false"); //Authenticator authenticator=new Authenticator ();Session session=session.getinstance (Pros); Session.setdebug (true); //set up a sent messagemimemessage msg=NewMimeMessage (session); //set up sender's mailbox and nameAddress toaddress=Newinternetaddress (sendemail,sendname);            Msg.setfrom (toaddress); //        /*address[] cc={new internetaddress (""), New InternetAddress ("")};        Msg.setrecipients (Message.RecipientType.CC, CC); */msg.setrecipients (Message.RecipientType.TO, UTO);//Set RecipientMsg.setsubject (topic);//ThemeMsg.settext (content);//contentMsg.setcontent (Content,"Text/html;charset=utf-8");//Send HTML-formatted messagesMsg.savechanges ();//Save Send//get the Send toolTransport ts=Session.gettransport (); //set up a connection mailbox host mailbox user name mailbox PasswordTs.connect (Connecthost,connectemailname,connectemailpass); Ts.sendmessage (MSG, msg.getallrecipients ());//SendTs.close ();//Close                        return true; }Else{            return false; }        }Catch(Exception e) {return false; }    }    }

Java Send mail

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.