SmtpClient发送邮件:An invalid character was found in the mail header

by kevin 8. 十月 2015 14:19 >
.net 4.0的环境下使用SmtpClient发送邮件,带有附件。碰到这个恶心的Exception System.FormatException: An invalid character was found in the mail header: 查了两天,终于解决了,是.Net自身的bug,据说后续版本是有解决的(估计.net 4.5以后)。 这里贴出解决方案: public static Attachment CreateAttachment(Stream attachmentFile, string displayName, string contentType, string attachmentFilePath) { var currentCulture = Thread.CurrentThread.CurrentCulture;//.net4.0 bug, var attachment = new Attachment(attachmentFile, displayName); try { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; attachment.ContentType = new ContentType(contentType); attachment.ContentDisposition.CreationDate = File.GetCreationTime(attachmentFilePath); attachment.ContentDisposition.ModificationDate = File.GetLastWriteTime(attachmentFilePath); attachment.ContentDisposition.ReadDate = File.GetLastAccessTime(attachmentFilePath); attachment.TransferEncoding = TransferEncoding.Base64; attachment.NameEncoding = Encoding.UTF8; string encodedAttachmentName = Convert.ToBase64String(Encoding.UTF8.GetBytes(displayName)); encodedAttachmentName = SplitEncodedAttachmentName(encodedAttachmentName); attachment.Name = encodedAttachmentName; } finally { Thread.CurrentThread.CurrentCulture = currentCulture; } return attachment; } private static string SplitEncodedAttachmentName(string encoded) { const string encodingtoken = "=?UTF-8?B?"; const string softbreak = "?="; const int maxChunkLength = 30; int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2); IEnumerable<string> parts = SplitByLength(encoded, splitLength); string encodedAttachmentName = encodingtoken; foreach (var part in parts) { encodedAttachmentName += part + softbreak + encodingtoken; } encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length); return encodedAttachmentName; } private static IEnumerable<string> SplitByLength(string stringToSplit, int length) { while (stringToSplit.Length > length) { yield return stringToSplit.Substring(0, length); stringToSplit = stringToSplit.Substring(length); } if (stringToSplit.Length > 0) { yield return stringToSplit; } } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 参考:https://support.microsoft.com/zh-cn/kb/2402064

开发杂记-烦心整理

by kevin 11. 十一月 2014 21:10 >
chown命令: 可以用来改变linux文件的所有者,用到这个,是因为交替使用ftp和winscp上传文件,而且使用了不同的用户,导致经常出现上传失败。 css的 line-height: 很多浏览器默认line-height的值是font-size的120%,也有是100%的,所有应该考虑reset一下。 php的日期比较: 过程化方法:date_diff 对象化方法:DateTimeObject->diff 这个没有.net那么舒服,有个TimeSpan,要获取相差多少天,多少秒的,真心麻烦。 直接上代码: 1: public static function datediff($dt_menor, $dt_maior, $str_interval, $relative=false) { 2: if (is_string($dt_menor)) 3: $dt_menor = date_create($dt_menor); 4: if (is_string($dt_maior)) 5: $dt_maior = date_create($dt_maior); 6:  7: $diff = date_diff($dt_menor, $dt_maior, !$relative); 8:  9: switch ($str_interval) { 10: case "y": 11: $total = $diff->y + $diff->m / 12 + $diff->d / 365.25; 12: break; 13: case "m": 14: $total = $diff->y * 12 + $diff->m + $diff->d / 30 + $diff->h / 24; 15: break; 16: case "d": 17: $total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h / 24 + $diff->i / 60; 18: break; 19: case "h": 20: $total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i / 60; 21: break; 22: case "i": 23: $total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s / 60; 24: break; 25: case "s": 26: $total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i) * 60 + $diff->s; 27: break; 28: } 29: if ($diff->invert) 30: return -1 * $total; 31: else 32: return $total; 33: }

开发杂记-潦草整理

by kevin 2. 九月 2014 23:15 >
css的float会将display为inline或者block的元素,修改成display:inline-block razor.parse 每次都会重新编译模板文件,这样做很耗资源(内存和CPU) Html.RenderPartial其实也是比较耗资源的 javascript,多个空格合并成一个: 1: string = string.replace(/\s{2,}/g, ' '); asp.net mvc,使用 razor模板引擎,让方法返回的字符串不会被进行Html编码: 1: public static IHtmlString BeginScript(this HtmlHelper htmlHelper) 2: { 3: return new HtmlString("<script type=\"text/javascript\">"); 4: } 5:  6: public static IHtmlString EndScript(this HtmlHelper htmlHelper) 7: { 8: return new HtmlString("</script>"); 9: } 导出Excel中出现乱码的终极解决方案: 1: <meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8"/> 2: <table> 3: <tr> 4: <td>cbcye@live.com | http://www.cbcye.com </td> … 5: </tr> 6: <table>

开发杂记-无心整理

by kevin 15. 六月 2014 17:32 >
最近是累了点,懒了点。 一个div元素,点击,页面外的元素,隐藏此div。 1: $(document).mouseup(function (e) 2: { 3: var container = $("YOUR CONTAINER SELECTOR"); 4:  5: if (!container.is(e.target) // if the target of the click isn't the container... 6: && container.has(e.target).length === 0) // ... nor a descendant of the container 7: { 8: container.hide(); 9: } 10: }); jquery.bxslider 是一个很不错的插件。 目前的版本是4+,使用过程中,碰到了几个bug. 如果刚好slider正在切换[gotoprev,gotonext]的时候,进行重绘[redraw],会造成slider停止工作。修复代码如下: 修改前: 1: el.redrawSlider = function () { 2: // resize all children in ratio to new screen size 3: slider.children.add(el.find('.bx-clone')).width(getSlideWidth()); 4: // adjust the height 5: slider.viewport.css('height', getViewportHeight()); 6: // update the slide position 7: if (!slider.settings.ticker) setSlidePosition(); 8: // if active.last was true before the screen resize, we want 9: // to keep it last no matter what screen size we end on 10: if (slider.active.last) slider.active.index = getPagerQty() - 1; 11: // if the active index (page) no longer exists due to the resize, simply set the index as last 12: if (slider.active.index >= getPagerQty()) slider.active.last = true; 13: // if a pager is being displayed and a custom pager is not being used, update it 14: if (slider.settings.pager && !slider.settings.pagerCustom) { 15: populatePager(); 16: updatePagerActive(slider.active.index); 17: } 18: } 修改后: 1: el.redrawSlider = function () { 2: if (slider.working) { 3: // Wait 'speed' amount of time before redrawing 4: setTimeout(function () { el.doRedrawSlider(); }, slider.settings.speed); 5: } else { 6: // Redraw immediately 7: el.doRedrawSlider(); 8: } 9: }; 10:  11: el.doRedrawSlider = function() { 12: // resize all children in ratio to new screen size 13: slider.children.add(el.find('.bx-clone')).width(getSlideWidth()); 14: // adjust the height 15: slider.viewport.css('height', getViewportHeight()); 16: // update the slide position 17: if (!slider.settings.ticker) setSlidePosition(); 18: // if active.last was true before the screen resize, we want 19: // to keep it last no matter what screen size we end on 20: if (slider.active.last) slider.active.index = getPagerQty() - 1; 21: // if the active index (page) no longer exists due to the resize, simply set the index as last 22: if (slider.active.index >= getPagerQty()) slider.active.last = true; 23: // if a pager is being displayed and a custom pager is not being used, update it 24: if (slider.settings.pager && !slider.settings.pagerCustom) { 25: populatePager(); 26: updatePagerActive(slider.active.index); 27: } 28: };   slider元素中,如果有img或者iframe无法正常加载,会引发slider无法正常工作。 修复前: 1: var loadElements = function (selector, callback) { 2: var total = selector.find('img, iframe').length; 3: if (total == 0) { 4: callback(); 5: return; 6: } 7: var count = 0; 8: selector.find('img, iframe').each(function () { 9: $(this).one('load', function () { 10: if (++count == total) callback(); 11: }).each(function () { 12: if (this.complete) $(this).load(); 13: }); 14: }); 15: } 修复后: 1: var loadElements = function (selector, callback) { 2: var total = selector.find('img, iframe').length; 3: if (total == 0) { 4: callback(); 5: return; 6: } 7: var count = 0; 8: selector.find('img, iframe').each(function () { 9: $(this).one('load', function () { 10: if (++count == total) callback(); 11: }).one('error', function() { 12: if (++count == total) callback(); 13: }).each(function () { 14: if (this.complete) $(this).load(); 15: }); 16: }); 17: }

开发杂记-未整理

by kevin 17. 三月 2014 22:27 >
window.location.replace window.location.replace('url'),简单的防止后退到前一个页面。 fluentData的ParameterOut fluentData的ParameterOut方法有bug 如果output decimal的话,小数点的位置会被设置为0,所以改用 Currency ParameterOut("SubTotal", DataTypes.Currency)

开发杂记-MSSQL Studio 技巧

by kevin 11. 三月 2014 14:52 >
关闭查询编辑器(SQL Query Editor)的查询结果窗口 快捷键是Ctrl+R 如果失效了,可以通过 Tools –> Options –> Environment –> Keyboard进行设置,如下图

实用的Dos命令

by kevin 23. 二月 2014 15:38 >
查看DNS缓存 ipconfig /displaydns 清除DNS缓存 ipconfig /flushdns

开发杂记:如何处理google chrome最小字号的问题

by kevin 16. 十二月 2013 14:15 >
最近在改版65emall的时候,碰到一个很棘手的问题,Chrome浏览器有个最小字号设置,就是当字号小于最小字号时,就会自动调整为自动调整为最小的字号。如下图: 在中文版的Chrome中,默认的最小字号是12; 在英文版的Chrome中,默认的最小字号是10。 1: -webkit-text-size-adjust:none; 网上很多都建议使用上面这样的语法来实现,但在高版本的Chrome中,这个语法已经无效了。 所以使用 -webkit-transform: 代码如下: 1: .font-size8 { font-size: 8px;} 2: .font-size9 {font-size: 9px;} 3: .font-size10 {font-size: 10px;} 4: @media screen and (-webkit-min-device-pixel-ratio:0) { 5: .font-size8 { 6: font-size: 12px; 7: margin-top: 1px; 8: -webkit-transform: scale(0.83); 9: } 10: .font-size9 { 11: font-size: 12px; 12: margin-top: 2px; 13: -webkit-transform: scale(0.86); 14: } 15: .font-size10 { 16: font-size: 12px; 17: margin-top: 2px; 18: -webkit-transform: scale(0.92); 19: } 20: } 注意一下 为了能同时兼容中英文Chrome,10号,11号也要这样缩小 这个比例不是简单的等比例缩小,比如9号字,的缩放比例就不是0.75 另外需要注意的一点是,长度,高度,还是会按照10或者12号字来计算,所以,最好在外层在加个div作为容器。 1: <div> 2: <div class="font-size9">font-size:9px</div> 3: </div>

开发杂记:让小于12号的字在google chrome中显示

by kevin 8. 十一月 2013 13:40 >
做为一个非专业的Css编写人员,一不小心,就可以踩到坑。 觉得小于12号的字不利于阅读,所以google chrome小于12号的字,都会被修正为12号。 google了下,据说,随着浏览器版本的升级,处理的方案还不一样。 整体的代码如下: 1: * { 2: -webkit-text-size-adjust: none; 3: -webkit-transform-origin-x: 0; 4: -webkit-transform: scale(0.8333333333333334); /* 10/12=0.8333333333333334) */ 5: }

开发杂记:z-index的那些坑

by kevin 22. 十月 2013 13:58 >
要实现my cart那样的边框效果,很自然的想到 上面一个div, 下面一个div, 上面那个div的z-index比下面的这个大, 下面这个div往上移1px。 然后,悲剧就开始了。。。 总结了一下,基本上是以下几个坑: z-index 仅能在定位元素上奏效,就是要求该元素设置 position:xxxx,否则z-index无效。 如果元素内嵌于li,那么li需要设置 position:relative 上面的div需要设置background,否则无法遮住下面的div 主要代码如下: 1: <li style="position:relative" id="cart" > 2: <div style="width: 100px; height:23px; position: relative; z-index: 2; background-color: white; border: 1px solid #d3d3d3;"></div> 3: <div style="position: absolute; width: 100px; z-index: 1; top: 22px; background-color: white; border: 1px solid #d3d3d3; "> 4: </div> 5: </li>