2017年8月25日 星期五

[.Net] Connect Exchange Server


為了要可以使用Exchage server寄信,必須從NuGet下載Exchange Web Services (EWS)
後來測試,怎麼連不都連不上,才發現原來有相依.Net的版本(好像要3.5以上)

另外一個要注意的是網址,用的是exchange.asmx,不是原本的xxx.com/owa/auth/logon.aspx
這個環節也試了很久,後來看到別人寫的範例都是.asmx,想說試看看就可以連了




Sample Code:

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);//版本預設值最新版
service.Credentials = new WebCredentials(ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"]);
service.Url = new Uri("https://xxxx.com/ews/exchange.asmx"); // Server路徑

EmailMessage email = new EmailMessage(service);
  
email.ToRecipients.Add(contact.Name, contact.Email); //收件者

email.CcRecipients.Add("AAA", "test1@mail.com"); //CC
email.BccRecipients.Add("BBB", "test2@mail.com "); //密件副本
email.Subject = mailJob.Subject; //主旨
email.Body = mailJob.Content; //內容
email.Body.BodyType = BodyType.HTML; //格式
email.Send();
Ref:
http://blog.oscarscode.com/dot-net/get-started-with-exchange-web-services-ews/
https://stackoverflow.com/questions/13517323/exchange-web-service-api-and-401-unauthorized-exception
https://blog.miniasp.com/post/2007/11/01/The-remote-certificate-is-invalid-according-to-the-validation-procedure.aspx

2016年10月14日 星期五

[SQL]Remove duplicates

This will also take care of duplicates (return one row for each user_id):


SELECT * FROM (
  SELECT u.*, FIRST_VALUE(u.rowid) OVER(PARTITION BY u.user_id ORDER BY u.date DESC) AS last_rowid
  FROM users u
) u2
WHERE u2.rowid = u2.last_rowid




Ref:
http://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column

2016年7月15日 星期五

[SQL SERVER] convert comma Separated String into rows




DECLARE @str VARCHAR(4000)= '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832,833,836,837,846,913,914,919,922,923,924,925,926,927,927,928,929,929,930,931,932,934,935,1029,1072,1187,1188,1192,1196,1197,1199,1199,1199,1199,1200,1201,1202,1203,1204,1205,1206,1207,1208,1209,1366,1367,1387,1388,1666,1759,1870,2042,2045,2163,2261,2374,2445,2550,2676,2879,2880,2881,2892,2893,2894';

SELECT t.c.value('.', 'VARCHAR(1000)')
FROM (
  SELECT x = CAST('' +
   REPLACE(@str , ',', '') + '' AS XML)
 ) a
CROSS APPLY x.nodes('/t') t(c);





Ref:

2016年6月21日 星期二

String.IsNullOrEmpty in JavaScript

Starting with:
return (!value || value == undefined || value == "" || value.length == 0);
Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it:
return (!value || value == undefined || value == "");
But wait! In JS, an empty string is false. Therefore, drop value == "":
return (!value || value == undefined);
And !undefined is true, so that check isn't needed. So we have:
return (!value);
And we don't need parentheses:
return !value
Q.E.D.


http://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript

2016年6月2日 星期四

[jQuery]add dynamic rows



Ref:
http://stackoverflow.com/questions/2145012/adding-rows-dynamically-with-jquery

http://ssiddique.info/different-ways-to-add-row-to-a-table-using-jquery.html

http://stackoverflow.com/questions/16183231/jquery-append-and-remove-dynamic-table-row

2016年5月27日 星期五

[MS SQL] SPRING JDBC


 -- 使用JDBC連線
http://www.codedata.com.tw/java/java-tutorial-the-3rd-class-2-spring-jdbc/

JDBCDataSource dataSource = new JDBCDataSource();
dataSource.setUrl("jdbc:hsqldb:file:src/main/resources/db/dvd_library");
dataSource.setUser("codedata");
dataSource.setPassword("123456");


-- 呼叫 PROCEDURE
 http://stackoverflow.com/questions/9361538/spring-jdbc-template-for-calling-stored-procedures


SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withSchemaName(schema)
    .withCatalogName(package)
    .withProcedureName(procedure)();
...
jdbcCall.addDeclaredParameter(new SqlParameter(paramName, OracleTypes.NUMBER));
...
jdbcCall.execute(callParams);




-- procedure 回傳 temp table

http://stackoverflow.com/questions/1443663/how-to-return-temporary-table-from-stored-procedure

 -- The "called" SP
  declare
      @returnAsSelect bit = 0;

  if object_id('tempdb..#GetValuesOutputTable') is null
  begin
      set @returnAsSelect = 1;
      create table #GetValuesOutputTable(
         ...   
      );
  end

  -- populate the table

  if @returnAsSelect = 1
      select * from #GetValuesOutputTable;

2016年5月16日 星期一

[JAVA8] truncate java 8 LocalDateTime



LocalDateTime now =  LocalDateTime.now();
LocalDateTime roundFloor =  now.truncatedTo(ChronoUnit.MINUTES);
LocalDateTime roundCeiling =  now.truncatedTo(ChronoUnit.MINUTES).plusMinutes(1);
http://stackoverflow.com/questions/25552023/round-minutes-to-ceiling-using-java-8