创建发送邮件客服端步骤:
1.创建Smtp服务器.
2.创建发件人,与收件人邮箱地址.
3.创建发送信息.
4.建件发送附件.
1.Smtp服务器,可用本地的也可以用其它的可用有Smtp服务器.如。smtp.126.com;smtp163.com.
需要注意的时:
(1)如果用本的的,本地要装Smtp用服也就是系统组件,默认没有安装。
(2)如果用邮件服务商的,你要有其上面的帐号与密码,即你要申请个邮箱,做为的身份认证。
(3)smtp类初始化时只能用IP而不能用用务器名称。因为我个人觉得,一般提供邮件服务都有好几台服务器。你在CMD 命令行里,ping smtp.126.com,在不同时候返回不同的IP。这个问题找了很久才发觉。所在在程序里要用先得到,Smtp服务器的IP列表,然后用PING 命令判断那个可用。然后再用此IP来初始化Smtp服务类。
取得SMTP服务器列表。
IPAddress[] IpAddress = Dns.GetHostEntry(ServerName).AddressList;
Ping ping = new Ping();
PingReply pingReply = null;
//取得smt服务器可用的IP
foreach (IPAddress IP in IpAddress)
{
pingReply = ping.Send(IP);
if (pingReply.Status == IPStatus.Success)
{
ip = IP.ToString();
break;
}
}
SmtpClient_my = new SmtpClient(ip, Port);
2.创建发件人,与收件人邮箱地址.
MailAddress_from = new MailAddress(Tb_Email_from.Text, Tb_Print.Text);
MailAddress_to = new MailAddress(Tb_Email_to.Text);
3.创建发送信息
MailMessage_my = new MailMessage();
MailMessage_my.Subject = Tb_Content.Text;
MailMessage_my.ReplyTo = MailAddress_from;
MailMessage_my.Sender = MailAddress_from;
MailMessage_my.From = MailAddress_from;
MailMessage_my.To.Add(MailAddress_to);
4.建件发送附件.
Attachment_my = new Attachment(path, MediaTypeNames.Application.Octet);
注意要控制附件的大小,因为很多邮件服务商附件大小都是有限件的。
FileStream FileStream_my = new FileStream(path, FileMode.Open);
string name = FileStream_my.Name;
int size = (int)(FileStream_my.Length / 1024);
//控制文件大小不大于10M
if (size > 10240)
{
MessageBox.Show("文件长度不能大于10M!你选择的文件大小为{0}", size.ToString());
return;
}
FileStream_my.Close();
5。发送后事件处理。
创建事件
SmtpClient_my.SendCompleted += new SendCompletedEventHandler(SmtpClient_my_SendCompleted);
事件处理函数。
void SmtpClient_my_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{ MessageBox.Show("发送已取消!"); }
if (e.Error != null)
{
MessageBox.Show(e.UserState.ToString() + "发送错误:" + e.Error.Message, "发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("邮件成功发出!", "恭喜!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endre
没有合适的资源?快使用搜索试试~ 我知道了~
c#发送邮件(sendMail)
共22个文件
cs:6个
exe:3个
txt:2个
4星 · 超过85%的资源 需积分: 10 23 下载量 113 浏览量
2009-02-01
13:47:07
上传
评论 1
收藏 45KB RAR 举报
温馨提示
创建发送邮件客服端步骤: 1.创建Smtp服务器. 2.创建发件人,与收件人邮箱地址. 3.创建发送信息. 4.建件发送附件.
资源推荐
资源详情
资源评论
收起资源包目录
SendEmail.rar (22个子文件)
SendEmail
发送邮件说明.txt 3KB
SendEmail.sln 916B
SendEmail.suo 13KB
SendEmail
Form1.cs 7KB
bin
Debug
SendEmail.vshost.exe 6KB
SendEmail.pdb 30KB
SendEmail.exe 28KB
obj
SendEmail.csproj.FileList.txt 282B
Debug
SendEmail.csproj.GenerateResource.Cache 842B
SendEmail.pdb 30KB
SendEmail.exe 28KB
SendEmail.Properties.Resources.resources 180B
Refactor
TempPE
SendEmail.Form1.resources 180B
Properties
Resources.Designer.cs 3KB
Settings.settings 249B
Resources.resx 5KB
Settings.Designer.cs 1KB
AssemblyInfo.cs 1KB
Program.cs 468B
Form1.Designer.cs 19KB
Form1.resx 6KB
SendEmail.csproj 3KB
共 22 条
- 1
资源评论
- u0100687152015-06-11谢谢推荐,我也写了一个发送邮件的小程序,不错。
- dwadadfdfdfdf2014-05-28有点小问题哦。。
joinler
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功