1 public class MySendMails
2 {
3 /// <summary>
4 /// 发送邮件
5 /// </summary>
6 /// <param name="addressee">收信人地址</param>
7 /// <param name="Subject">标题</param>
8 /// <param name="body">内容</param>
9 /// <returns></returns>
10 public static void Send(string address, string subject, string body)
11 {
12 ///读取config获得使用的邮箱信息
13 System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();
14 string from = appReader.GetValue("fromMail", typeof(string)).ToString();//发件人登陆帐户
15 string pwd = appReader.GetValue("mailPwd", typeof(string)).ToString();//发件人邮箱登陆密码
16 string user = appReader.GetValue("mailUser", typeof(string)).ToString();//发件人
17
18 MailMessage mail = new MailMessage(from, address, subject, body);
19 SmtpClient smtp = new SmtpClient();
20 smtp.Host = appReader.GetValue("mailSmtp", typeof(string)).ToString();//获得SMTP端口
21 smtp.Credentials = new System.Net.NetworkCredential(user, pwd);//邮箱账号与密码
22 smtp.Send(mail);//发送邮件
23 }
24 }
2 {
3 /// <summary>
4 /// 发送邮件
5 /// </summary>
6 /// <param name="addressee">收信人地址</param>
7 /// <param name="Subject">标题</param>
8 /// <param name="body">内容</param>
9 /// <returns></returns>
10 public static void Send(string address, string subject, string body)
11 {
12 ///读取config获得使用的邮箱信息
13 System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();
14 string from = appReader.GetValue("fromMail", typeof(string)).ToString();//发件人登陆帐户
15 string pwd = appReader.GetValue("mailPwd", typeof(string)).ToString();//发件人邮箱登陆密码
16 string user = appReader.GetValue("mailUser", typeof(string)).ToString();//发件人
17
18 MailMessage mail = new MailMessage(from, address, subject, body);
19 SmtpClient smtp = new SmtpClient();
20 smtp.Host = appReader.GetValue("mailSmtp", typeof(string)).ToString();//获得SMTP端口
21 smtp.Credentials = new System.Net.NetworkCredential(user, pwd);//邮箱账号与密码
22 smtp.Send(mail);//发送邮件
23 }
24 }
1<!--配置文件-->
2 <appSettings>
3 <add key="fromMail" value="邮件地址"/>
4 <add key="mailUser" value="发件人"/>
5 <add key="mailPwd" value="密码"/>
6 <add key="mailSmtp" value="smtp.163.com"/>
7 </appSettings>
2 <appSettings>
3 <add key="fromMail" value="邮件地址"/>
4 <add key="mailUser" value="发件人"/>
5 <add key="mailPwd" value="密码"/>
6 <add key="mailSmtp" value="smtp.163.com"/>
7 </appSettings>
经过测试绝对可用,但对发件邮箱有一定的限制,需要支持SMTP,现在大部分免费邮箱都不支持此功能,但2005年以前的163邮箱可以。