Note: Mail sent code, mail server application and configuration is a major link, bloggers here with the QQ mail server. There is a need to Google, Baidu to check how to open.
Today read the next spring Official document mail to send this section. Record the results of your first study here. The detailed usage scenarios are as follows:
1. Request a mailbox server for sending mail.
2. Introducing a jar package in the project to support Java mail delivery
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version>
</dependency><dependency> <groupId>javax.activation</groupId> <artifactid >activation</artifactId> <version>1.1.1</version></dependency>
3. Configure the Java Mail transmitter
Mail transmitter configuration: applicationcontext-mail.xml
<!--Define Mail Sender util bean--<bean id= "MailSender"class= "Org.springframework.mail.javamail.JavaMailSenderImpl" > <property name= "Protocol" value= "#{config[" Mail.protocol ']} "/> <property name=" host "value=" #{config[' Mail.host ']} "/> <property name=" por T "value=" #{config[' Mail.port '} "/> <property name=" username "value=" #{config[' Mail.username ']} "/> <property name= "Password" value= "#{config[' Mail.password ']}"/> <property name= "defaultencoding" value= " #{config[' mail.default.encoding '} "/> <property name=" javamailproperties "> <props> <prop key= "Mail.smtp.auth" >#{config[' Mail.smtp.auth ']}</prop> <prop key= "Mail.smt P.starttls.enable ">#{config[' mail.smtp.starttls.enable ']}</prop> <prop key=" Mail.smtp.timeout "& gt;#{config[' mail.smtp.timeout ']}</prop> </props> </property> </bean>
Mail Sender Properties File
mail.protocol=smtpmail.host=smtp.qq.commail.port=587mail.username= Sender mailbox Mail.password= mailbox Authorization mail. default. encoding=utf-8Mail.smtp.auth=truemail.smtp.starttls.enable= Truemail.smtp.timeout=25000
4. Writing a mail-sending processor
Interface:
/*** Send Text Email * *@paramEmail Recipients email address *@paramSubject *@paramcontent *@returnSend mail result*/ Public BooleansendText (string email, string subject, string content); /*** Send email with attachment * *@paramEmail *@paramSubject *@paramcontent *@paramAttachments *@return */
Public BooleanSendattachment (string email, string subject, string content, file...attachments);
1). Send text messages
MimeMessage MimeMessage =Mailsender.createmimemessage (); //indicate this is multipart message & indicate encodingMimemessagehelper helper =NewMimemessagehelper (MimeMessage,true, encoding); Helper.setfrom (form); //Set SenderHelper.setto (to);//Set RecipientsHelper.setsubject (subject); Helper.settext (content,true);//indicate the text included is HTML//Send MailMailsender.send (MimeMessage);
2). Send mail with Attachments
MimeMessage MimeMessage =Mailsender.createmimemessage (); //indicate this is multipart message & indicate encodingMimemessagehelper helper =NewMimemessagehelper (MimeMessage,true, encoding); Helper.setfrom (form); //Set SenderHelper.setto (to);//Set RecipientsHelper.setsubject (subject); Helper.settext (content,true);//indicate the text included is HTML//indicate with attachment for(File attachment:attachments) {helper.addattachment (Attachment.getname (), attachment);} //Send MailMailsender.send (MimeMessage);
Spring Mail Send 1