代码语言
.
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
】
C# 邮件发送
作者:
Dezai.CN
/ 发布于
2011/12/6
/
617
<div><span style="color: rgb(0,0,255)">using</span> System; <span style="color: rgb(0,0,255)">using</span> System.Collections.Generic; <span style="color: rgb(0,0,255)">using</span> System.ComponentModel; <span style="color: rgb(0,0,255)">using</span> System.Data; <span style="color: rgb(0,0,255)">using</span> System.Drawing; <span style="color: rgb(0,0,255)">using</span> System.Text; <span style="color: rgb(0,0,255)">using</span> System.Windows.Forms; <span style="color: rgb(0,0,255)">using</span> System.Net; <span style="color: rgb(0,0,255)">using</span> System.Net.Mail; <span style="color: rgb(0,0,255)">using</span> System.Net.Mime; <span style="color: rgb(0,0,255)">using</span> System.IO; <span style="color: rgb(0,0,255)">namespace</span> SendMailExample { <span style="color: rgb(128,128,128)">///</span> <span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 作者:Andrew </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> Blog: </span><span style="color: rgb(0,128,0); text-decoration: underline">http://blog.csdn.net/Andrew_wx</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span> <span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">partial</span> <span style="color: rgb(0,0,255)">class</span> FormSendMail : Form { <span style="color: rgb(0,0,255)">public</span> FormSendMail() { InitializeComponent(); } <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">void</span> FormSendMail_Load(<span style="color: rgb(0,0,255)">object</span> sender, EventArgs e) { txtSmtpServer.Text = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">smtp.qq.com</span><span style="color: rgb(128,0,0)">"</span>; txtSend.Text = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">heuandmei@qq.com</span><span style="color: rgb(128,0,0)">"</span>; txtDisplayName.Text = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Andrew(王旭)</span><span style="color: rgb(128,0,0)">"</span>; txtPassword.Text = <span style="color: rgb(128,0,0)">""</span>;<span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">密码 </span><span style="color: rgb(0,128,0)"> </span> txtReceive.Text = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">heuandmei@qq.com</span><span style="color: rgb(128,0,0)">"</span>; txtTitle.Text = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">发信测试</span><span style="color: rgb(128,0,0)">"</span>; txtBody.Text = <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">This is a test(测试)</span><span style="color: rgb(128,0,0)">"</span>; rbtnNoSSL.Checked = <span style="color: rgb(0,0,255)">true</span>; } <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">void</span> btnAddFiles_Click(<span style="color: rgb(0,0,255)">object</span> sender, EventArgs e) { OpenFileDialog odlg = <span style="color: rgb(0,0,255)">new</span> OpenFileDialog(); odlg.CheckFileExists = <span style="color: rgb(0,0,255)">true</span>; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">只接收有效的文件名 </span><span style="color: rgb(0,128,0)"> </span> odlg.ValidateNames = <span style="color: rgb(0,0,255)">true</span>; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">允许一次选择多个文件作为附件 </span><span style="color: rgb(0,128,0)"> </span> odlg.Multiselect = <span style="color: rgb(0,0,255)">true</span>; <span style="color: rgb(0,0,255)">if</span> (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { lstFiles.Items.AddRange(odlg.FileNames); } } <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">void</span> btnSend_Click(<span style="color: rgb(0,0,255)">object</span> sender, EventArgs e) { <span style="color: rgb(0,0,255)">this</span>.Cursor = Cursors.WaitCursor; MailMessage mail = <span style="color: rgb(0,0,255)">new</span> MailMessage(); mail.From = <span style="color: rgb(0,0,255)">new</span> MailAddress( txtSend.Text, txtDisplayName.Text, Encoding.UTF8); mail.To.Add(txtReceive.Text); mail.Subject = txtTitle.Text; mail.SubjectEncoding = Encoding.Default; mail.Body = txtBody.Text; mail.BodyEncoding = Encoding.Default; mail.IsBodyHtml = <span style="color: rgb(0,0,255)">false</span>; mail.Priority = MailPriority.Normal; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">添加附件 </span><span style="color: rgb(0,128,0)"> </span> Attachment attachment = <span style="color: rgb(0,0,255)">null</span>; <span style="color: rgb(0,0,255)">if</span> (lstFiles.Items.Count > <span style="color: rgb(128,0,128)">0</span>) { <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(0,0,255)">int</span> i = <span style="color: rgb(128,0,128)">0</span>; i < lstFiles.Items.Count; i++) { <span style="color: rgb(0,0,255)">string</span> pathFileName = lstFiles.Items[i].ToString(); <span style="color: rgb(0,0,255)">string</span> extName = Path.GetExtension(pathFileName).ToLower(); <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">判断附件类型 </span><span style="color: rgb(0,128,0)"> </span> <span style="color: rgb(0,0,255)">if</span> (extName == <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">.rar</span><span style="color: rgb(128,0,0)">"</span> || extName == <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">.zip</span><span style="color: rgb(128,0,0)">"</span>) { attachment = <span style="color: rgb(0,0,255)">new</span> Attachment(pathFileName, MediaTypeNames.Application.Zip); } <span style="color: rgb(0,0,255)">else</span> { attachment = <span style="color: rgb(0,0,255)">new</span> Attachment(pathFileName, MediaTypeNames.Application.Octet); } ContentDisposition cd = attachment.ContentDisposition; cd.CreationDate = File.GetCreationTime(pathFileName); cd.ModificationDate = File.GetLastWriteTime(pathFileName); cd.ReadDate = File.GetLastAccessTime(pathFileName); mail.Attachments.Add(attachment); } } SmtpClient client = <span style="color: rgb(0,0,255)">new</span> SmtpClient(); client.Host = txtSmtpServer.Text; client.Port = <span style="color: rgb(128,0,128)">25</span>; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">是否使用安全套接字层加密连接 </span><span style="color: rgb(0,128,0)"> </span> client.EnableSsl = rbtnUseSSL.Checked; <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">不使用默认凭证,注意此句必须放在 client.Credentials 的上面 </span><span style="color: rgb(0,128,0)"> </span> client.UseDefaultCredentials = <span style="color: rgb(0,0,255)">false</span>; client.Credentials = <span style="color: rgb(0,0,255)">new</span> NetworkCredential(txtSend.Text, txtPassword.Text); <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">邮件通过网络直接发送到服务器 </span><span style="color: rgb(0,128,0)"> </span> client.DeliveryMethod = SmtpDeliveryMethod.Network; <span style="color: rgb(0,0,255)">try</span> { client.Send(mail); MessageBox.Show(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">发送成功</span><span style="color: rgb(128,0,0)">"</span>); } <span style="color: rgb(0,0,255)">catch</span> (SmtpException ex) { MessageBox.Show(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">发送失败:</span><span style="color: rgb(128,0,0)">"</span> + ex.Message); } <span style="color: rgb(0,0,255)">catch</span> (Exception ex) { MessageBox.Show(<span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">发送失败:</span><span style="color: rgb(128,0,0)">"</span> + ex.Message); } <span style="color: rgb(0,0,255)">finally</span> { mail.Dispose(); client = <span style="color: rgb(0,0,255)">null</span>; <span style="color: rgb(0,0,255)">this</span>.Cursor = Cursors.Default; } } } }
试试其它关键字
邮件发送
同语言下
.
文件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