Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Blog Archive
Stats
Register User Controls and Custom Controls in Web.config
  1. <system.web>
  2.     <pages>
  3.         <controls>
  4.             <add tagPrefix="scottgu" src="~/Controls/Header.ascx" tagName="header"/>
  5.             <add tagPrefix="scottgu" src="~/Controls/Footer.ascx" tagName="footer"/>
  6.             <add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
  7.         </controls>
  8.     </pages>
  9. </system.web>
Parse query string
  1. Uri url = new Uri("http://www.google.com.tw/search?sourceid=chrome&ie=UTF-8&q=uri");
  2.  
  3. System.Collections.Specialized.NameValueCollection queryString
  4.     = System.Web.HttpUtility.ParseQueryString(url.Query);
  5.  
  6. Console.WriteLine(queryString.Get("sourceid"));
  7. Console.WriteLine(queryString.Get("ie"));
script path in master page
use script manager (script will appear in body section)
  1. <asp:ScriptManager ID="ScriptManager1" runat="server">
  2.     <Scripts>
  3.         <asp:ScriptReference Path="~/jquery.js" />
  4.     </Scripts>
  5. </asp:ScriptManager>
use ResolveClientUrl method
  1. <script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
asp.net form validate
  1. // 確認更新
  2. function confirmUpdate(
  3.     confirmMessage, /* 確認訊息 */
  4.     validateGroup, /* validate group*/
  5.     fn /* 自訂欄位檢查function */
  6. ) {
  7.     if ('undefined' != typeof (tinyMCE)) tinyMCE.triggerSave(); // 觸發tinyMCE儲存值
  8.     if ('undefined' != typeof (validateGroup)) validateGroup = null;
  9.     if ('function' != typeof (fn)) fn = function () { return true; }  // 自訂驗證的function, 若無則回傳true
  10.     if ('function' != typeof (Page_ClientValidate)) var Page_ClientValidate = function (group) { return true; } // validate control驗證
  11.     if ($(document.forms[0]).valid2() && Page_ClientValidate(validateGroup)) {
  12.         return fn() && confirm(confirmMessage);
  13.     } else {
  14.         return false;
  15.     }
  16. }
Retire value from tinyMCE when jQuery validate valid() invoked
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="superspace.usercontrols.WebForm2" ValidateRequest="false" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title>tinyMCE + jQuery validate</title>
  7.     <!-- jQuery -->
  8.     <script src="<%=VirtualPathUtility.ToAbsolute("~/")%>js/jquery-1.6.2.min.js" type="text/javascript"></script>
  9.     <!-- jQuery validate + metadata -->
  10.     <script src="<%=VirtualPathUtility.ToAbsolute("~/")%>js/jquery.validate/jquery.metadata.min.js" type="text/javascript"></script>
  11.     <script src="<%=VirtualPathUtility.ToAbsolute("~/")%>js/jquery.validate/jquery.validate.min.js" type="text/javascript"></script>
  12.     <script src="<%=VirtualPathUtility.ToAbsolute("~/")%>js/jquery.validate/jquery.validate.ext.min.js" type="text/javascript"></script>
  13.     <!-- tinyMCE Editor -->
  14.     <script src="<%=VirtualPathUtility.ToAbsolute("~/")%>js/tiny_mce/tiny_mce.js" type="text/javascript"></script>
  15.     <script src="<%=VirtualPathUtility.ToAbsolute("~/")%>js/tiny_mce/jquery.tinymce.js" type="text/javascript"></script>
  16.     <style type="text/css">
  17.         body
  18.         {
  19.             font-size: smaller;
  20.         }
  21.     </style>
  22.     <script type="text/javascript">
  23.         $(function () {
  24.             $('.txtContent').tinymce({
  25.                 theme: "advanced",
  26.                 language: "zh",
  27.                 theme_advanced_toolbar_location: "top",
  28.                 theme_advanced_toolbar_align: "left",
  29.                 theme_advanced_statusbar_location: "bottom",
  30.                 theme_advanced_resizing: true
  31.             });
  32.             $(document.forms).validate();
  33.         });
  34.         function confirmUpdate() {
  35.             if (typeof (tinyMCE) != 'undefined') tinyMCE.triggerSave();
  36.             if (!$(document.forms).valid()) return false;
  37.             return true;
  38.         }
  39.     </script>
  40. </head>
  41. <body>
  42.     <form id="form1" runat="server">
  43.     <h3>
  44.         Force tinyMCE editor to save content to control, avoid validate error when valid() has been invoked;</h3>
  45.     <table border="0">
  46.         <tr>
  47.             <td>
  48.                 <b style="color: Red;">*</b>標題
  49.             </td>
  50.             <td>
  51.                 <asp:TextBox ID="txtTitle" runat="server" CssClass="{required:true,messages:{required:'請填寫標題!'}}" />
  52.             </td>
  53.         </tr>
  54.         <tr>
  55.             <td valign="top">
  56.                 <b style="color: Red;">*</b>內容
  57.             </td>
  58.             <td>
  59.                 <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" Width="100%" Rows="15" CssClass="txtContent {required:true,messages:{required:'請填寫內容!'}}" />
  60.             </td>
  61.         </tr>
  62.         <tr>
  63.             <td align="center" colspan="2">
  64.                 <asp:Button ID="btnSubmit" runat="server" Text="儲存" OnClientClick="return confirmUpdate();" />
  65.             </td>
  66.         </tr>
  67.     </table>
  68.     </form>
  69. </body>
  70. </html>
jQuery validator: focus invalid field when calling valid()
  1. /*
  2.  * 在validate呼叫valid()驗證方法式, 加上focusInValid的動件
  3.  */
  4. (function($) {
  5.     $.extend($.fn, {
  6.         valid2 : function() {
  7.             var valid = true;
  8.             var validator;
  9.             if ($(this[0]).is('form')) {
  10.                 validator = this.validate();
  11.                 valid = this.validate().form();
  12.             } else {
  13.                 validator = $(this[0].form).validate();
  14.                 this.each(function() {
  15.                     valid &= validator.element(this);
  16.                 });
  17.             }
  18.             if (!valid)
  19.                 validator.focusInvalid();
  20.             return valid;
  21.         }
  22.     });
  23.  
  24.     // 新增一個 regex 的驗證方式
  25.     $.validator.methods.regex = function(value, element, param) {
  26.         return this.optional(element) || ((typeof(param) == 'function' && typeof(param.test) == 'function') ? param.test(value) : new RegExp(param).test(value));
  27.     };
  28.     $.validator.messages.regex = 'Please enter a valid value.';
  29. })(jQuery);
Add a event to Outlook calendar dymatically
  1. protected void btnAddEventToOutlook_Click(object sender, EventArgs e)
  2. {
  3.     Response.Clear();
  4.     Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.ics", HttpUtility.UrlEncode(EventName, Response.HeaderEncoding)));
  5.  
  6.     Response.Write(string.Format(@"BEGIN:VCALENDAR
  7. METHOD:PUBLISH
  8. PRODID:-//{0}//NONSGML iCalcreator 2.6//ZH_TW
  9. VERSION:2.0
  10. X-WR-CALNAME;LANGUAGE=zh_tw:有 {1} 筆相關活動
  11. BEGIN:VEVENT
  12. UID:{2}
  13. DTSTAMP:{3:yyyyMMddTHHmmss}Z
  14. DESCRIPTION;LANGUAGE=zh_tw:{4}
  15. DTSTART;TZID=TAIWAN:{5:yyyyMMddTHHmmss}
  16. DTEND;TZID=TAIWAN:{6:yyyyMMddTHHmmss}
  17. LOCATION;LANGUAGE=zh_tw:{7}
  18. SUMMARY;LANGUAGE=zh_tw:{8}
  19. URL;VALUE=URI:{9}
  20. END:VEVENT
  21. END:VCALENDAR",
  22.         Request.Url.Host,
  23.         PlaceCount,
  24.         Request.QueryString["oid"],
  25.         DateTime.Now,
  26.         Description,
  27.         Convert.ToDateTime(FromDate),
  28.         Convert.ToDateTime(ToDate),
  29.         Location,
  30.         EventName,
  31.         Request.Url));
  32.     Response.End();
  33. }
about ics file
Add a event to Google Calendar dynamically
  1. hlGoogle.NavigateUrl = string.Format(
  2.     "https://www.google.com/calendar/render?action=TEMPLATE&text={0}&dates={1:yyyyMMddTHHmmss}/{2:yyyyMMddTHHmmss}&sprop=website:{3}&location={4}&details={5}",
  3.     EventName,
  4.     Convert.ToDateTime(FromDate),
  5.     Convert.ToDateTime(ToDate),
  6.     Request.Url.Host,
  7.     Location,
  8.     Details
  9. );
ObjectDataSource
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ObjectDataSourceSample.aspx.cs"
  2.     Inherits="superspace.control.info.ObjectDataSourceSample" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:HiddenField ID="hdnContentId" runat="server" />
  13.         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
  14.             DeleteMethod="Delete"
  15.             InsertMethod="Insert"
  16.             SelectMethod="Select"
  17.             UpdateMethod="Update"
  18.             OldValuesParameterFormatString="{0}"
  19.             TypeName="SuperspaceLib.VoteOption">
  20.             <DeleteParameters>
  21.                 <asp:Parameter Name="ContentId" Type="String" />
  22.                 <asp:Parameter Name="OptionID" Type="String" />
  23.             </DeleteParameters>
  24.             <InsertParameters>
  25.                 <asp:Parameter Name="ContentId" Type="String" />
  26.                 <asp:Parameter Name="OptionID" Type="String" />
  27.                 <asp:Parameter Name="OptionName" Type="String" />
  28.             </InsertParameters>
  29.             <SelectParameters>
  30.                 <asp:ControlParameter ControlID="hdnContentId" Name="ContentId" PropertyName="Value"
  31.                     Type="String" />
  32.             </SelectParameters>
  33.             <UpdateParameters>
  34.                 <asp:Parameter Name="ContentId" Type="String" />
  35.                 <asp:Parameter Name="OptionID" Type="String" />
  36.                 <asp:Parameter Name="OptionName" Type="String" />
  37.             </UpdateParameters>
  38.         </asp:ObjectDataSource>
  39.         <asp:TextBox ID="txtVoteOption" runat="server" MaxLength="50" Style="width: 100%;" />
  40.         <asp:Button ID="btnAddOption" runat="server" Text="新增" OnClick="btnAddOption_Click" />
  41.         <asp:GridView ID="gvOptions" runat="server" AutoGenerateColumns="False" GridLines="None"
  42.             ShowHeader="False" DataKeyNames="ContentId,OptionID" EnableModelValidation="True"
  43.             DataSourceID="ObjectDataSource1">
  44.             <Columns>
  45.                 <asp:BoundField DataField="OptionName" HeaderText="OptionName" SortExpression="OptionName" />
  46.                 <asp:TemplateField ShowHeader="False">
  47.                     <ItemTemplate>
  48.                         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
  49.                             Text="編輯"></asp:LinkButton>
  50.                         &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
  51.                             Text="刪除"></asp:LinkButton>
  52.                     </ItemTemplate>
  53.                     <EditItemTemplate>
  54.                         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
  55.                             Text="更新"></asp:LinkButton>
  56.                         &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
  57.                             Text="取消"></asp:LinkButton>
  58.                     </EditItemTemplate>
  59.                 </asp:TemplateField>
  60.             </Columns>
  61.         </asp:GridView>
  62.     </div>
  63.     </form>
  64. </body>
  65. </html>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Web;
  8.  
  9. namespace SuperspaceLib
  10. {
  11.     [DataObject(true)]
  12.     public class VoteOption
  13.     {
  14.         [DataObjectMethod(DataObjectMethodType.Select)]
  15.         public DataTable Select(string ContentId)
  16.         {
  17.             DataTable dtOption = null;
  18.             if (HttpContext.Current.Session[ContentId] == null)
  19.             {
  20.                 dtOption = new DataTable("VoteOption");
  21.                 dtOption.Columns.Add("ContentId");
  22.                 dtOption.Columns.Add("OptionID");
  23.                 dtOption.Columns.Add("OptionName");
  24.             }
  25.             else
  26.             {
  27.                 dtOption = HttpContext.Current.Session[ContentId] as DataTable;
  28.             }
  29.             return dtOption;
  30.         }
  31.  
  32.         [DataObjectMethod(DataObjectMethodType.Update)]
  33.         public int Update(string ContentId, string OptionID, string OptionName)
  34.         {
  35.             DataTable dtOption = null;
  36.             if (HttpContext.Current.Session[ContentId] == null)
  37.             {
  38.                 dtOption = new DataTable("VoteOption");
  39.                 dtOption.Columns.Add("ContentId");
  40.                 dtOption.Columns.Add("OptionID");
  41.                 dtOption.Columns.Add("OptionName");
  42.             }
  43.             else
  44.             {
  45.                 dtOption = HttpContext.Current.Session[ContentId] as DataTable;
  46.             }
  47.  
  48.             int afftected = 0;
  49.             foreach (DataRow row in dtOption.Rows)
  50.             {
  51.                 if (row["ContentId"].Equals(ContentId) && row["OptionID"].Equals(OptionID))
  52.                 {
  53.                     row["OptionName"] = OptionName;
  54.                     afftected++;
  55.                 }
  56.             }
  57.             HttpContext.Current.Session[ContentId] = dtOption;
  58.  
  59.             return afftected;
  60.         }
  61.  
  62.         [DataObjectMethod(DataObjectMethodType.Insert)]
  63.         public int Insert(string ContentId, string OptionID, string OptionName)
  64.         {
  65.             DataTable dtOption = null;
  66.             if (HttpContext.Current.Session[ContentId] == null)
  67.             {
  68.                 dtOption = new DataTable("VoteOption");
  69.                 dtOption.Columns.Add("ContentId");
  70.                 dtOption.Columns.Add("OptionID");
  71.                 dtOption.Columns.Add("OptionName");
  72.             }
  73.             else
  74.             {
  75.                 dtOption = HttpContext.Current.Session[ContentId] as DataTable;
  76.             }
  77.  
  78.             DataRow row = dtOption.Rows.Add(ContentId, OptionID, OptionName);
  79.             HttpContext.Current.Session[ContentId] = dtOption;
  80.  
  81.             return 1;
  82.         }
  83.  
  84.         [DataObjectMethod(DataObjectMethodType.Delete)]
  85.         public int Delete(string ContentId, string OptionID)
  86.         {
  87.             DataTable dtOption = null;
  88.             if (HttpContext.Current.Session[ContentId] == null)
  89.             {
  90.                 dtOption = new DataTable("VoteOption");
  91.                 dtOption.Columns.Add("ContentId");
  92.                 dtOption.Columns.Add("OptionID");
  93.                 dtOption.Columns.Add("OptionName");
  94.             }
  95.             else
  96.             {
  97.                 dtOption = HttpContext.Current.Session[ContentId] as DataTable;
  98.             }
  99.  
  100.             int afftected = 0;
  101.             for (int i = dtOption.Rows.Count - 1; i >= 0; i--)
  102.             {
  103.                 DataRow row = dtOption.Rows[i];
  104.                 if (row["ContentId"].Equals(ContentId) && row["OptionID"].Equals(OptionID))
  105.                 {
  106.                     dtOption.Rows.Remove(row);
  107.                     afftected++;
  108.                 }
  109.             }
  110.             HttpContext.Current.Session[ContentId] = dtOption;
  111.  
  112.             return afftected;
  113.         }
  114.     }
  115. }
HINT:
  • OldValuesParameterFormatString="{0}" means parameter will take value of old value by this name format
  • Parameter name must compeletly match the param of method
UpdatePanel in Formview occurs duplicate component id error
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InnerUpdatePanel.aspx.cs"
  2.     Inherits="superspace.control.info.InnerUpdatePanel" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:FormView ID="fvVote" runat="server" DefaultMode="Insert">
  13.             <InsertItemTemplate>
  14.                 <asp:TextBox ID="txtVoteName" runat="server" />
  15.                 <asp:UpdatePanel ID="upOption" runat="server" UpdateMode="Conditional">
  16.                     <ContentTemplate>
  17.                         <table border="0" width="100%">
  18.                             <tr>
  19.                                 <td>
  20.                                     <asp:TextBox ID="txtOption" runat="server" />
  21.                                 </td>
  22.                                 <td>
  23.                                     <asp:Button ID="btnAddOption" runat="server" Text="新增選項" />
  24.                                 </td>
  25.                             </tr>
  26.                         </table>
  27.                     </ContentTemplate>
  28.                 </asp:UpdatePanel>
  29.             </InsertItemTemplate>
  30.             <EditItemTemplate>
  31.                 <asp:TextBox ID="txtVoteName" runat="server" />
  32.                 <asp:UpdatePanel ID="upOption" runat="server" UpdateMode="Conditional">
  33.                     <ContentTemplate>
  34.                         <table border="0" width="100%">
  35.                             <tr>
  36.                                 <td>
  37.                                     <asp:TextBox ID="txtOption" runat="server" />
  38.                                 </td>
  39.                                 <td>
  40.                                     <asp:Button ID="btnAddOption" runat="server" Text="新增選項" />
  41.                                 </td>
  42.                             </tr>
  43.                         </table>
  44.                     </ContentTemplate>
  45.                 </asp:UpdatePanel>
  46.             </EditItemTemplate>
  47.         </asp:FormView>
  48.     </div>
  49.     </form>
  50. </body>
  51. </html>
Solutions:
  1. Remove update panel
  2. Create a user control wrapping update panel
Test mail sender
  1. import java.awt.Dimension;
  2. import java.awt.GridBagConstraints;
  3. import java.awt.GridBagLayout;
  4. import java.awt.Insets;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.io.PrintStream;
  10.  
  11. import javax.swing.BorderFactory;
  12. import javax.swing.JButton;
  13. import javax.swing.JCheckBox;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JPasswordField;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextArea;
  20. import javax.swing.JTextField;
  21.  
  22. import bruce.lib.swing.SwingWrench;
  23. import bruce.lib.util.DateTime;
  24. import bruce.lib.util.PostCenter;
  25.  
  26. public class PostMan extends JFrame {
  27.  
  28.     JTextField txtIP;
  29.     JTextField txtPort;
  30.     JTextField txtAccount;
  31.     JPasswordField txtPassword;
  32.  
  33.     JTextField txtSenderMail;
  34.     JTextField txtSednerName;
  35.     JTextField txtReceiverMail;
  36.  
  37.     JCheckBox cbIsHtml;
  38.     JTextField txtSubject;
  39.     JTextArea txtBody;
  40.  
  41.     JButton btnSubmit;
  42.  
  43.     JTextArea txtConsole;
  44.  
  45.     public PostMan() {
  46.         setLayout(new GridBagLayout());
  47.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.         setTitle("發信測試");
  49.  
  50.         JPanel control = new JPanel(new GridBagLayout());
  51.         control.setMinimumSize(new Dimension(350, 100));
  52.  
  53.         control.add(new JLabel("主機ip"), new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  54.         control.add(txtIP = new JTextField("ms2.ccic.com.tw", 10), new GridBagConstraints(2, 1, 1, 1, 0.3, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  55.         control.add(new JLabel("通訊埠"), new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  56.         control.add(txtPort = new JTextField("25", 3), new GridBagConstraints(4, 1, 1, 1, 0.3, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  57.  
  58.         control.add(new JLabel("帳號"), new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  59.         control.add(txtAccount = new JTextField("gp_report", 10), new GridBagConstraints(2, 2, 1, 1, 0.3, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  60.         control.add(new JLabel("密碼"), new GridBagConstraints(3, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  61.         control.add(txtPassword = new JPasswordField("29952666", 10), new GridBagConstraints(4, 2, 1, 1, 0.3, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  62.  
  63.         control.add(new JLabel("寄件人名稱"), new GridBagConstraints(1, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  64.         control.add(txtSednerName = new JTextField("MES系統訊息", 10), new GridBagConstraints(2, 3, 1, 1, 0.3, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  65.         control.add(new JLabel("寄件人mail"), new GridBagConstraints(3, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  66.         control.add(txtSenderMail = new JTextField("gp_report@ccic.com.tw", 10), new GridBagConstraints(4, 3, 1, 1, 0.3, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  67.  
  68.         control.add(new JLabel("收件人"), new GridBagConstraints(1, 4, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  69.         control.add(txtReceiverMail = new JTextField("", 10), new GridBagConstraints(2, 4, 3, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  70.  
  71.         control.add(new JLabel("html格式"), new GridBagConstraints(1, 5, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  72.         control.add(cbIsHtml = new JCheckBox(), new GridBagConstraints(2, 5, 3, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  73.  
  74.         control.add(new JLabel("主旨"), new GridBagConstraints(1, 6, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  75.         control.add(txtSubject = new JTextField("", 10), new GridBagConstraints(2, 6, 3, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));
  76.         control.add(new JScrollPane(txtBody = new JTextArea()), new GridBagConstraints(1, 7, 4, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 1, 1));
  77.  
  78.         add(control, new GridBagConstraints(1, 1, 1, 2, 0, 1, GridBagConstraints.EAST, GridBagConstraints.VERTICAL, new Insets(3, 3, 3, 3), 1, 1));
  79.  
  80.         add(new JLabel("LOG"), new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  81.         add(btnSubmit = new JButton("發送"), new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 1, 1));
  82.         add(new JScrollPane(txtConsole = new JTextArea()), new GridBagConstraints(2, 2, 2, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 1, 1));
  83.  
  84.         btnSubmit.addActionListener(btnSubmit_Click);
  85.  
  86.         // redirect console
  87.         System.setOut(new PrintStream(new OutputStream() {
  88.  
  89.             StringBuffer buffer = new StringBuffer();
  90.  
  91.             @Override
  92.             public void write(int b) throws IOException {
  93.                 switch (b) {
  94.                 case '\n':
  95.                     txtConsole.append(buffer.toString() + System.getProperty("line.separator"));
  96.                     buffer.delete(0, buffer.length() - 1);
  97.                     break;
  98.                 default:
  99.                     buffer.append((char) b);
  100.                     break;
  101.                 }
  102.             }
  103.         }));
  104.  
  105.         setSize(600, 400);
  106.         SwingWrench.setLocationCenter(this);
  107.     }
  108.  
  109.     private ActionListener btnSubmit_Click = new ActionListener() {
  110.         @Override
  111.         public void actionPerformed(ActionEvent e) {
  112.  
  113.             try {
  114.                 System.out.printf("==========Try sending mail at %s==========%n", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
  115.  
  116.                 PostCenter<PostCenter> pc = new PostCenter<PostCenter>();
  117.  
  118.                 pc.init(txtIP.getText().trim(), Integer.parseInt(txtPort.getText().trim()), true, txtAccount.getText().trim(), txtPassword.getText().trim());
  119.                 pc.setDebug(true);
  120.                 pc.setDebugOut(System.out);
  121.                 pc.setEncoding("UTF-8");
  122.  
  123.                 if (txtSednerName.getText().trim().length() == 0)
  124.                     pc.setSender(txtSenderMail.getText().trim());
  125.                 else
  126.                     pc.setSender(txtSenderMail.getText().trim(), txtSednerName.getText().trim());
  127.  
  128.                 String[] receivers = txtReceiverMail.getText().replaceAll("\\s", "").replaceAll(",", ";").split(";");
  129.  
  130.                 for (String s : receivers) {
  131.                     pc.addReceiver(s);
  132.                 }
  133.  
  134.                 pc.send(txtSubject.getText(), txtBody.getText(), cbIsHtml.isSelected());
  135.             } catch (Exception ex) {
  136.                 ex.printStackTrace(System.out);
  137.             }
  138.  
  139.             System.out.println();
  140.             System.out.println();
  141.             System.out.println();
  142.         }
  143.     };
  144.  
  145.     /**
  146.      * @param args
  147.      */
  148.     public static void main(String[] args) {
  149.         // TODO Auto-generated method stub
  150.         new PostMan().setVisible(true);
  151.     }
  152.  
  153. }