SPRINGMVC Mail Sending

Source: Internet
Author: User

<!--mail delivery implementation class--

<bean id= "Javamailsender" class= "Org.springframework.mail.javamail.JavaMailSenderImpl" >

<property name= "host" value= "smtp.qq.com"/>

<property name= "Port" value= "/>"

<property name= "username" value= "your Mailbox"/>

<property name= "Password" value= "Your mailbox password"/>

<property name= "Javamailproperties" >

<props >

<prop key= "Mail.smtp.auth" >true</prop>

</props>

</property>

</bean>

<bean id= "Simplemailmessage" class= "Org.springframework.mail.SimpleMailMessage" >

<property name= "from" value= "sender"/>

<property name= "Subject" value= "Mail Subject"/>

<!--

<property name= "text" value= "Mail Content"/>

-

</bean>

<!--thread pool sending mail asynchronously--

<bean id= "Taskexecutor" class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >

<property name= "Corepoolsize" value= "5"/>

<property name= "Maxpoolsize" value= "ten"/>

<property name= "queuecapacity" value= "/>"

</bean>

<!--freemarker Templates--

<bean id= "Freemarkerconfigurer" class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >

<property name= "Templateloaderpath" value= "Classpath:template"/>

<!--<property name= "Templateloaderpath" value= "/template"/>-->

<property name= "Freemarkersettings" >

<props>

<prop key= "Locale" >zh_CN</prop>

<prop key= "Default_encoding" >UTF-8</prop>

</props>

</property>

</bean>

Tool classes for sending messages: (using annotations as a class for managing mail delivery)

public class MailSender {

@Inject

Private Javamailsender Javamailsender;

@Inject

Private Simplemailmessage simplemailmessage;

@Inject

Private Freemarkerconfigurer Freemarkerconfigurer;

@Inject

Private Taskexecutor Taskexecutor;

/**

* Build message content and send mail.

* @param user

* @param URL link

*/

public void Send (user user, String url,string email) {

String nickname = User.getnickname ();

String to = email;

String text = "";

map<string, string> map = new hashmap<string, string> (1);

Map.put ("url", url);

try {

Generate message content from Freemarker templates

Template template = Freemarkerconfigurer.getconfiguration (). GetTemplate ("REGISTER_MAIL.FTL");

${XXX} in the template, the value of key XXX in map replaces the placeholder content.

Text = freemarkertemplateutils.processtemplateintostring (template, map);

} catch (IOException e) {

E.printstacktrace ();

} catch (Templateexception e) {

E.printstacktrace ();

}

This.taskExecutor.execute (New Sendmailthread (To,null,text));

}

An internal threading class that uses the thread pool to send messages asynchronously.

Private class Sendmailthread implements Runnable {

Private String to;

Private String subject;

Private String content;

Private Sendmailthread (string to, string subject, string content) {

Super ();

This.to = to;

This.subject = subject;

this.content = content;

}

@Override

public void Run () {

SendMail (to, subject, content);

}

}

/**

* Send mail

* @param to Recipient mailbox

* @param subject Mail subject

* @param content messages

*/

public void SendMail (string to, string subject, string content) {

try {

MimeMessage message = Javamailsender.createmimemessage ();

Mimemessagehelper messagehelper = new Mimemessagehelper (message, True, "UTF-8");

Messagehelper.setfrom (Simplemailmessage.getfrom ());

if (subject! = NULL) {

Messagehelper.setsubject (subject);

} else {

Messagehelper.setsubject (Simplemailmessage.getsubject ());

}

Messagehelper.setto (to);

Messagehelper.settext (content, true);

Javamailsender.send (message);

} catch (Messagingexception e) {

E.printstacktrace ();

}

}

SPRINGMVC Mail Sending

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.