C# WinFrom 发送邮件
C# Winforms 发送邮件

发送邮件时用到以下来个命名空间:

using System.Net;

using System.Net.Mail;

发送邮件的发信人邮箱必须使用smtp协议的邮箱,例如163、126、sina 等邮箱(但是163、226的早期注册的用户使用了些协议,126邮箱从06年6六月份后的注册的邮箱都不可用,而sina邮箱里可以自己设置是否启用smtp协议)其它邮箱类同

以126为例 smtp服务器为:smtp.126.com

以下为程序截图:

以下为全部代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.Net.Mail;

namespace Mail

{

public partial class mailone : Form

{

public mailone()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

if (fjr.Text.Length == 0 || sjr.Text.Length == 0)

{

MessageBox.Show("请填写Email地址", "填写不完整", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

return;

}

try

{

YanZheng(fjr.Text, sjr.Text); //验证邮件格式

}

catch (EmailErrorException ex)

{

MessageBox.Show(ex.Message, "Email格式错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

return;

}

string fjrtxt = fjr.Text;

string mmtxt = mm.Text;

string sjrtxt = sjr.Text;

string zttxt = zt.Text;

string fjtxt = fj.Text;

string nrtxt = nr.Text;

string[] fasong = fjrtxt.Split('@');

string[] fs = fasong[1].Split('.');

//发送

SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com"); //设置邮件协议

client.UseDefaultCredentials = false;//这一句得写前面

client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器

client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt); //通过用户名和密码 认证

MailMessage mmsg = new MailMessage(new MailAddress(fjrtxt), new MailAddress(sjrtxt)); //发件人和收件人的邮箱地址

mmsg.Subject = zttxt; //邮件主题

mmsg.SubjectEncoding = Encoding.UTF8; //主题编码

mmsg.Body = nrtxt; //邮件正文

mmsg.BodyEncoding = Encoding.UTF8; //正文编码

mmsg.IsBodyHtml = true; //设置为HTML格式

mmsg.Priority = MailPriority.High; //优先级

if (fj.Text.Trim() != "")

{

mmsg.Attachments.Add(new Attachment(fj.Text));//增加附件

}

try

{

client.Send(mmsg);

MessageBox.Show("邮件已发成功");

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void button3_Click(object sender, EventArgs e)

{

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

fj.Text = openFileDialog1.FileName; //得到附件的地址

}

}

private void button2_Click(object sender, EventArgs e)

{

this.Close();

}

private bool YanZheng(string fmail, string smail)

{

string[] subStrings = smail.Split('@');

string[] subStrings1 = fmail.Split('@');

if (subStrings.Length != 2 || subStrings1.Length != 2)

{

throw new EmailErrorException();

}

else

{

int index = subStrings[1].IndexOf(".");

int index1 = subStrings1[1].IndexOf(".");

if (index <= 0 || index1 <= 0)

{

throw new EmailErrorException();

}

if (subStrings[1][subStrings[1].Length - 1] == '.' || subStrings1[1][subStrings[1].Length - 1] == '.')

{

throw new EmailErrorException();

}

}

return true;

}

}

}

//以下为程序类文件,用于验证填写邮箱地址是否正确

using System;

using System.Collections.Generic;

using System.Text;

namespace Mail

{

public class EmailErrorException : ApplicationException

{

public string _message;

public EmailErrorException()

: base()

{

_message = null;

}

public EmailErrorException(string message)

: base()

{

_message = message.ToString();

}

public EmailErrorException(string message, Exception myNew)

: base(message, myNew)

{

_message = message.ToString();

}

public override string Message

{

get

{

return "Email格式错误。";

}

}

}

}

最新文章

  1. 转→js数组遍历 千万不要使用for...in...
  2. java-final关键字
  3. PAT 1004. 成绩排名 (20)
  4. Centos 7 通过挂载系统光盘搭建本地yum仓库的方法
  5. [公告]这里的博客将不再更新,最新博客请移步至blog.coderzh.com
  6. 修改sqlserver2008默认的数据库文件保存路径
  7. Flip Game I &amp;&amp; II
  8. 简述负载均衡&amp;CDN技术
  9. android软件开发之webView.addJavascriptInterface循环渐进【一】
  10. C# MVC 自学笔记—2 MVC Movie简介
  11. &lt;经验杂谈&gt;Mysql中字符串处理的几种处理方法concat、concat_ws、group_concat
  12. 关于Class文件
  13. github常见操作和常见错误!错误提示:fatal: remote origin already exist
  14. Idea主题下载
  15. xml 转换成对象(采用反射机制对对对象属性赋值)
  16. mysql-5.5.50-winx64
  17. 洛谷1894 [USACO4.2]完美的牛栏The Perfect Stall
  18. OpenNebula学习第三节之虚拟机管理
  19. 使用Xshell和Xftfp部署简单的项目
  20. 汇编入门学习笔记 (七)—— dp,div,dup

热门文章

  1. Centos7 -samba服务配置
  2. Spring Framework基础学习
  3. Eureka-server配置servlet.context-path后导致Eureka-client注册到server问题
  4. STM32 debug setting 闪退
  5. HDU 6649 Data Structure Problem(凸包+平衡树)
  6. JavaScript之基础语法
  7. 小白学Python(11)——pyecharts,绘制饼图 Pie
  8. Gorgeous Sequence(线段树)
  9. ReactiveCocoa详解
  10. 第二讲shiro异常及执行流程