代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
异步发送Email
作者:
Dezai.CN
/ 发布于
2011/6/22
/
752
<div><blockquote> <div><span class="stx-comment">//</span><span class="stx-comment">/ <summary></span> <span id="l405"> <span class="stx-comment">//</span><span class="stx-comment">/ Sends the mail to gmail through STunnel</span> </span><span id="l406"> <span class="stx-comment">//</span><span class="stx-comment">/ </summary></span> </span><span id="l407"> <span class="stx-comment">//</span><span class="stx-comment">/ <param name="fileInfo"></param></span> </span><span id="l408"> <span class="stx-comment">//</span><span class="stx-comment">/ <param name="filename"></param></span> </span><span id="l409"> <span class="stx-comment">//</span><span class="stx-comment">/ <param name="fullPath"></param></span> </span><span id="l410"> <span class="stx-keyword">private</span> <span class="stx-keyword">void</span> SendMail(FileInfo fileInfo, <span class="stx-keyword">string</span> filename, <span class="stx-keyword">string</span> fullPath) </span><span id="l411"> { </span><span id="l412"> <span class="stx-keyword">if</span> (Settings.FullDirectoryPathInSubject) </span><span id="l413"> { </span><span id="l414"> filename = fullPath; </span><span id="l415"> } </span><span id="l416"> </span><span id="l417"> mailSent = <span class="stx-keyword">false</span>; </span><span id="l418"> </span><span id="l419"> MailAddress toAddress = <span class="stx-keyword">new</span> MailAddress(Settings.EmailTo); </span><span id="l420"> MailAddress fromAddress = <span class="stx-keyword">new</span> MailAddress(Settings.EmailFrom); </span><span id="l421"> </span><span id="l422"> MailMessage mail = <span class="stx-keyword">new</span> MailMessage(fromAddress, toAddress); </span><span id="l423"> mail.Subject = <span class="stx-keyword">string</span>.Format(<span class="stx-string">"{0} {1}"</span>, Settings.EmailFilter, filename); ; </span><span id="l424"> mail.Body = filename; </span><span id="l425"> Attachment attachment = <span class="stx-keyword">new</span> Attachment(fullPath); </span><span id="l426"> mail.Attachments.Add(attachment); </span><span id="l427"> </span><span id="l428"> SmtpClient smtpClient = <span class="stx-keyword">new</span> SmtpClient(Settings.SmtpServer, Settings.SmtpPort); </span><span id="l429"> smtpClient.SendCompleted += <span class="stx-keyword">new</span> SendCompletedEventHandler(smtpClient_SendCompleted);</span><span id="l431"> </span><span id="l432"> smtpClient.<span id="m0" class="GOYGFLXBKH">SendAsync</span>(mail, fileInfo); </span><span id="l433"> </span><span id="l434"> <span class="stx-keyword">while</span> (!mailSent) </span><span id="l435"> { </span><span id="l436"> Thread.Sleep(<span class="stx-number">500</span>); </span><span id="l437"> } </span><span id="l438"> </span><span id="l439"> mail.Dispose(); </span><span id="l440"> }</span></div> <pre><span id="l497"></span></pre> <pre><span id="l497"> <span class="stx-comment">//</span><span class="stx-comment">/ <summary></span> </span><span id="l498"> <span class="stx-comment">//</span><span class="stx-comment">/ This is called once the email is sent successfully</span> </span><span id="l499"> <span class="stx-comment">//</span><span class="stx-comment">/ </summary></span> </span><span id="l500"> <span class="stx-comment">//</span><span class="stx-comment">/ <param name="sender"></param></span> </span><span id="l501"> <span class="stx-comment">//</span><span class="stx-comment">/ <param name="e"></param></span> </span><span id="l502"> <span class="stx-keyword">void</span> smtpClient_SendCompleted(<span class="stx-keyword">object</span> sender, System.ComponentModel.AsyncCompletedEventArgs e) </span><span id="l503"> { </span><span id="l504"> <span class="stx-comment">//</span><span class="stx-comment"> Get the unique identifier for this asynchronous operation.</span> </span><span id="l505"> FileInfo fileInfo = (FileInfo)e.UserState; </span><span id="l507"> <span class="stx-keyword">string</span> filename = Path.GetFileName(fileInfo.FullName); </span><span id="l508"> Console.WriteLine(<span class="stx-string">">>>> File {0} sent"</span>, filename); </span><span id="l510"> <span class="stx-keyword">if</span> (e.Error != <span class="stx-keyword">null</span>) </span><span id="l511"> { </span><span id="l512"> Console.WriteLine(<span class="stx-string">"[{0}] {1}"</span>, fileInfo.FullName, e.Error.ToString()); </span><span id="l513"> } </span><span id="l515"> mailSent = <span class="stx-keyword">true</span>; </span><span id="l516"> }</span></pre> </blockquote></div>
试试其它关键字
发送Email
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3