|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。Spring供应了一个发送电子邮件的初级笼统层,它向用户屏障了底层邮件体系的一些细节,同时卖力低条理的代表客户真个资本处置。Spring邮件笼统层的次要包为org.springframework.mail。它包含了发送电子邮件的次要接口MailSender和封装了复杂邮件的属性如from,to,cc,subject,text的值对象叫做SimpleMailMessage。
1、我们界说一个发送邮件的接口:OrderManager.java
1publicinterfaceOrderManagerextendsBaseManager{
2 /**
3 *email,要发送的邮件地点;
4 *Code:激活码
5 */
6 publicvoidplaceOrder(Stringemail);
7}
2、我们必要对该接口举行完成的办法:OrderManagerImpl.java
1importjavax.mail.Message;
2importjavax.mail.MessagingException;
3importjavax.mail.internet.InternetAddress;
4importjavax.mail.internet.MimeMessage;
5importorg.springframework.mail.MailException;
6importorg.springframework.mail.javamail.JavaMailSender;
7importorg.springframework.mail.javamail.MimeMessagePreparator;
8importservice.OrderManager;
9
11publicclassOrderManagerImplextendsBaseManagerImplimplementsOrderManager{
12
13 privateJavaMailSendermailsender;
14 privateMyMailMessagemessage;
15
16
17 publicvoidsetMessage(CityMailMessagemessage)
18 {
19 this.message=message;
20 }
21 publicvoidsetMailsender(JavaMailSendermailsender){
22 this.mailsender=mailsender;
23 }
24 publicvoidplaceOrder(finalStringemail){
25
26
27 MimeMessagePreparatorpreparator=newMimeMessagePreparator(){
28 publicvoidprepare(MimeMessagemimeMessage)throwsMessagingException{
29 mimeMessage.setRecipient(Message.RecipientType.TO,
30 newInternetAddress(email));
31 mimeMessage.setFrom(newInternetAddress(message.getFrom()));
32 /**转换编码为GBK*/
33 mimeMessage.setSubject(message.getSubject(),"GBK");
36 mimeMessage.setText(email+"<br>"+message.getSubject()+message.getText(),"GBK");
37
38 }
39 };
40 try{
41 mailsender.send(preparator);
42 }
43 catch(MailExceptionex){
44 //logitandgoon
45 System.err.println(ex.getMessage());
46 }
47}
48}
3、spring设置发送email的applicationContext-email.xml
1<?xmlversion="1.0"encoding="UTF-8"?>
2<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
3"http://www.springframework.org/dtd/spring-beans.dtd">
4
5<beans>
6 <beanid="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl">
7 <propertyname="host">
8 <value>smtp.163.com</value>
9 </property>
10 <propertyname="username">
11 <value>username</value>
12 </property>
13 <propertyname="password">
14 <value>password</value>
15 </property>
16<propertyname="javaMailProperties">
17 <props>
18 <propkey="mail.smtp.auth">true</prop>
19 <propkey="mail.smtp.timeout">25000</prop>
20 </props>
21</property>
22</bean>
23
24<beanid="mailMessage"class="org.springframework.mail.SimpleMailMessage">
25 <propertyname="from">
26 <value>Email</value>
27 </property>
28 <propertyname="subject">
29 <value>题目</value>
30 </property>
31 <propertyname="text">
32 <value>内容</value>
33 </property>
46</bean>
47
48<beanid="orderManager"class="cn.cityyouth.service.impl.OrderManagerImpl">
49 <propertyname="mailsender">
50 <refbean="mailSender"/>
51 </property>
52 <propertyname="message">
53 <refbean="mailMessage"/>
54 </property>
55</bean>
56
57</beans>
4、最初设置本人的jsp页面和action
1packagecn.cityyouth.web.action;
2
3importjavax.servlet.http.HttpServletRequest;
4importjavax.servlet.http.HttpServletResponse;
5importorg.apache.struts.action.ActionForm;
6importorg.apache.struts.action.ActionForward;
7importorg.apache.struts.action.ActionMapping;
8importorg.apache.struts.action.ActionMessage;
9importorg.apache.struts.action.ActionMessages;
10importcom.test.service.OrderManager;
11
12publicclassSendMailActionextendsBaseAction{
13
14 /**
15 *Methodexecute
16 *
17 *@parammapping
18 *@paramform
19 *@paramrequest
20 *@paramresponse
21 *@returnActionForward
22 */
23publicActionForwardexecute(ActionMappingmapping,ActionFormform,
24HttpServletRequestrequest,HttpServletResponseresponse){
25 OrderManageromi=(OrderManager)this.getBean("orderManager");
26 Stringuseremail="123@163.com";
27 omi.placeOrder(useremail);
28}
29}
到此一切的开辟以停止。
Sring邮件笼统层的次要包是:org.springframework.mail包。它包括叫MailSender为发送邮件的中心接口和包括复杂邮件属性比方from,to,cc,subject,text叫SimpleMailMessage的值对象.这个包也包括一个反省非常的条理,它撑持一个更初级其余笼统凌驾初级其余邮件体系非常陪伴根非常存在MailException.请参考JavaDocs为更多的信息杂邮件非常条理。
Spring也撑持一个MailSender的公用于JavaMail特性比方MIME动静子接口,定名为org.springframework.javamail.JavaMailer。它也撑持一个为JavaMailMIME信息的筹办回调接口,定名为org.springframework.mail.JavaMail.MimeMessagePreparator.
JAVA是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由升阳(SunMicrosystems)公司的詹姆斯·高斯林(JamesGosling)等人于1990年代初开发。 |
|