Spring Mail Send 1

Source: Internet
Author: User
Tags starttls

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

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.