首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Flex兑现简单的Email发送

2012-11-25 
Flex实现简单的Email发送?? 本文主要介绍和实现怎么使用Flex-java来实现Email的发送。这尤其是在做用户回馈

Flex实现简单的Email发送

?

? 本文主要介绍和实现怎么使用Flex-java来实现Email的发送。这尤其是在做用户回馈的时候需要用到。当然这实现的只要是一个简单的例子。正在执行发送Email功能还是使用java来实现的,Flex只不过提供了数据的输入和封装。使用的原理很简单:Flex-Form <--接受用户输入?? ||?? ||使用RemoteObject方式就用户输入的数据封装成json格式传递到后台java bean?? ||Java Bean()--->执行真正的Email发送----->回馈消息---->Flex-Form使用的界面如下图:Flex兑现简单的Email发送简单实现了用户输入的验证以及Email验证(更详细的可以参考Adobe数据验证)以下是实现的代码:1.后台java处理:【你需要两个jar: mail.jar and activation.jar】
package mail;import javax.mail.*;import javax.mail.internet.*;import java.util.*;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class SendFlexMail {     private static final String SMTP_HOST_NAME = "smtp.163.com";     private static final String SMTP_AUTH_USER = "feixianryexin";     private static final String SMTP_AUTH_PWD = "yourpassword";     // Add List of Email address to who email needs to be sent to     private String[] emailList;      public String sendFlexMail(String flexMail){         String result="";         JSONArray flexJavaMail = JSONArray.fromObject(flexMail);         emailList = new String[2];         JSONObject mailJsonObj = JSONObject.fromObject(flexJavaMail.getString(0));         emailList[0] = mailJsonObj.getString("senderEmail");//send mail to yourself         emailList[1] = mailJsonObj.getString("sendToEmail");         String senderName = mailJsonObj.getString("senderName");         String sendToName = mailJsonObj.getString("sendToName");         String senderMail = mailJsonObj.getString("senderEmail");         String emailMessage = mailJsonObj.getString("emailMessage");         String emailTopic = sendToName+":A Letter From Your Friend "+senderName;         try{         postMail(emailList,emailTopic, emailMessage,senderMail);         result="Congratulations!";         }catch (MessagingException e){             e.printStackTrace();         }         return result;      }     public void postMail( String recipients[ ], String subject,     String message , String from) throws MessagingException     {     boolean debug = false;     //Set the host smtp address     Properties props = new Properties();     props.put("mail.smtp.host", SMTP_HOST_NAME);     props.put("mail.smtp.auth", "true");     Authenticator auth = new SMTPAuthenticator();     Session session = Session.getDefaultInstance(props, auth);     session.setDebug(debug);     // create a message     Message msg = new MimeMessage(session);     // set the from and to address     InternetAddress addressFrom = new InternetAddress(from);     msg.setFrom(addressFrom);     InternetAddress[] addressTo = new InternetAddress[recipients.length];     for (int i = 0; i < recipients.length; i++)     {     addressTo[i] = new InternetAddress(recipients[i]);     }     msg.setRecipients(Message.RecipientType.TO, addressTo);     // Setting the Subject and Content Type     msg.setSubject(subject);     msg.setContent(message, "text/plain");     Transport.send(msg);     }    /**    * SimpleAuthenticator is used to do simple authentication    * when the SMTP server requires it.    */    private class SMTPAuthenticator extends javax.mail.Authenticator    {     public PasswordAuthentication getPasswordAuthentication()     {     String username = SMTP_AUTH_USER;     String password = SMTP_AUTH_PWD;     return new PasswordAuthentication(username, password);     }    }}
?2.前台的Flex-Form:??
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html"><mx:Script>    <![CDATA[        import mx.controls.Alert;        import mx.controls.TextInput;        import mx.rpc.events.ResultEvent         import mx.collections.ArrayCollection;        import com.adobe.serialization.json.JSON;                private var flexMail:ArrayCollection;        public function sendMailAction( ):void        {                                flexMail = new ArrayCollection();           var mailObj:Object = new Object();           mailObj.senderName = senderName.text;           mailObj.senderEmail =senderEmail.text;           mailObj.sendToName = sendToName.text;           mailObj.sendToEmail = sendToEmail.text;           mailObj.emailMessage =emailMessage.text;                      //check email valid           if(senderName.text !="" && senderEmail.text!=""&&sendToName.text!=""&& sendToEmail.text!=""&& emailMessage.text!=""){                 if(senderEmail.text.indexOf("@")==-1 || sendToEmail.text.indexOf("@")==-1){                   labState.text="Email格式不正确哦!";                 }else{                     flexMail.addItem(mailObj);                      var emailString:String = JSON.encode(flexMail.toArray());                     sendMail.sendFlexMail(emailString)                     labState.text="正在发送请稍候...";           }           }else{                labState.text="请输入所有的选项!";           }        }                 public function sendFlexMailResult(event:ResultEvent):void        {            Alert.show(event.result as String);            senderName.text="";            senderEmail.text="";            sendToName.text="";            sendToEmail.text="";            emailMessage.text="";            labState.text="恭喜,成功发送邮件!";        }            ]]></mx:Script><mx:Style source="mailFormCSS.css" /><mx:RemoteObject id="sendMail" destination="sendFlexMail" showBusyCursor="true" result="sendFlexMailResult(event)" />    <mx:Panel width="448" height="474" >        <mx:Text width="100%" height="29" text="Email Form" styleName="header"/>        <mx:Text text="Flex3 / BlazeDS / Java" styleName="subHeader" />        <mx:HRule width="100%"/>                 <mx:Form id="emailForm" width="100%" >                      <mx:FormItem label="Your Name:" horizontalAlign="left" width="100%">                <mx:TextInput id="senderName" width="100%"/>            </mx:FormItem>                        <mx:FormItem label="Your E-mail:" width="100%">                <mx:TextInput id="senderEmail" width="100%"/>            </mx:FormItem>                     <mx:FormItem label="Friends Name:" horizontalAlign="left" width="100%">                <mx:TextInput id="sendToName" width="100%"/>            </mx:FormItem>                <mx:FormItem label="Friends E-mail:" width="100%">                <mx:TextInput id="sendToEmail" width="100%"/>            </mx:FormItem>                        <mx:FormItem label="Message" horizontalAlign="left" width="100%">                <mx:TextArea id="emailMessage" width="100%" height="100"/>            </mx:FormItem>                         <mx:FormItem width="100%" height="33">                <mx:Spacer width="200" />                <mx:Button id="submitBtn" label="Submit" click="sendMailAction()"/>             </mx:FormItem>        </mx:Form>             <mx:HBox styleName="cbStyle" width="414">      <mx:Label id="labState" width="393" fontSize="14" color="#FE0113"/>           <!-- <mx:CheckBox label="Check to send a copy to yourself" id="cb"/>-->        </mx:HBox>                <mx:HRule width="100%" height="0"/>                <mx:Label styleName="footer">               <mx:htmlText><![CDATA[<a href="http://yexin218.cublog.cn" target="_blank" ><b>一路风尘制作</b></a>]]></mx:htmlText>           </mx:Label>        </mx:Panel>    </mx:Application>
?

数据传递使用的是BlazeDS.关于BlazeDS参考:使用BlazeDS实现Java和Flex通信

附件下载【包含Flex-plugin的eclipse工程文件以及源码】:DownLoad

1 楼 yexin218 2008-06-16   我一直在想,Flex本身有没有发送Email的功能呢?知道的朋友,不妨相告。谢谢 2 楼 hacker47 2008-07-13   FLEX几乎只是客户端的解决方案,AIR中有对数据库操作的相关类库,可以目前支持差强人意,据我所知,FLEX自身还不能发送EMAIL.

热点排行