It takes two jar packages to send a message in Java.
Activation.jar
Mail.jar
Mailutils.java
9 Importjava.io.IOException;Ten One Importjava.util.List; A - Importjava.util.Properties; - the - - ImportJavax.mail.Authenticator; - + ImportJavax.mail.Message.RecipientType; - + Importjavax.mail.MessagingException; A at Importjavax.mail.PasswordAuthentication; - - Importjavax.mail.Session; - - ImportJavax.mail.Transport; - in Importjavax.mail.internet.InternetAddress; - to ImportJavax.mail.internet.MimeBodyPart; + - ImportJavax.mail.internet.MimeMessage; the * ImportJavax.mail.internet.MimeMultipart; $ Panax Notoginseng Importjavax.mail.internet.MimeUtility; - the + A /** the + * - $ * @authorThis class has only one way to send mail! $ - */ - the Public classMailutils { - Wuyi Public StaticSession CreateSession (String host,FinalString username,FinalString Password) { the -Properties prop =NewProperties (); Wu -Prop.setproperty ("Mail.host", host);//Specify host About $Prop.setproperty ("Mail.smtp.auth", "true");//Specifies that validation is true - - - A //creating validators + theAuthenticator auth =NewAuthenticator () { - $ Publicpasswordauthentication getpasswordauthentication () { the the return Newpasswordauthentication (username, password); the the } - in }; the the //Get Session Object About the returnsession.getinstance (prop, auth); the the } + - /** the Bayi * Send the specified message the the * - - * @paramMail the the */ the the Public Static voidSend (Session session,FinalMail mail)throwsMessagingexception, - the IOException { the the 94 theMimeMessage msg =NewMimeMessage (session);//Create a Mail object the theMsg.setfrom (NewInternetAddress (Mail.getfrom ()));//Set Sender98 AboutMsg.addrecipients (Recipienttype.to, mail.gettoaddress ());//Set Recipient - 101 102 103 //set up cc104 theString cc =mail.getccaddress ();106 107 if(!Cc.isempty ()) {108 109 msg.addrecipients (recipienttype.cc, CC); the 111 } the 113 the the //set Dark Send the 117String bcc =mail.getbccaddress ();118 119 if(!Bcc.isempty ()) { - 121 msg.addrecipients (RECIPIENTTYPE.BCC, BCC);122 123 }124 the 126 127Msg.setsubject (Mail.getsubject ());//Set Theme - 129 the 131Mimemultipart parts =NewMimemultipart ();//Create a part set object the 133 134 135MimeBodyPart part =NewMimeBodyPart ();//Create a part136 137Part.setcontent (Mail.getcontent (), "text/html;charset=utf-8");//Set Message text content138 139Parts.addbodypart (part);//add a part to a part set $ 141 ///////////////////////////////////////////142 143 144 145 //Add an attachment146 147list<attachbean> attachbeanlist = Mail.getattachs ();//Get all Attachments148 149 if(Attachbeanlist! =NULL) { Max 151 for(Attachbean attach:attachbeanlist) { the 153MimeBodyPart Attachpart =NewMimeBodyPart ();//Create a part154 155Attachpart.attachfile (Attach.getfile ());//set up attachment files156 157 Attachpart.setfilename (mimeutility.encodetext (Attach158 159. GetFileName ()));//Set the attachment file name the 161String cid =attach.getcid ();162 163 if(CID! =NULL) {164 165 Attachpart.setcontentid (CID);166 167 }168 169 Parts.addbodypart (attachpart); the 171 }172 173 }174 175 176 177Msg.setcontent (parts);//set content to messages178 179Transport.send (msg);//send e-mail the 181 }182 183 }184 185
Mailutiltest.java
Mailutiltest.java /* * 1. Log in to the mail server * Mailutils.createsession (server address, login name, password); * 2. Create Message Object * Sender * recipient * Subject * body * 3. **/=new Mail (" Sender "," Recipient "," subject "," Body "); // Send Mailutils.send (session, mail);
Java Send mail