现在的位置: 首页 > 综合 > 正文

发邮件的小程序

2012年05月11日 ⁄ 综合 ⁄ 共 2601字 ⁄ 字号 评论关闭

这个好像前两三个月前做的东西了,当然也那里对于发邮箱也不是很了解,在网上也参考一了些例子。具体的地址、作者已经不记得了,如果看到了你有的东西,记得留言给我,我会加上你的名称,一来是尊重,二来是感谢! 好了,下面直接贴代码好了。

default.aspx.cs

1 if (!Page.IsPostBack)
2             {
3                 //调用发邮件的方法
4                 Class1.Test("FormMail@163.com""ToMail@qq.com""Receive Name""smtp.163.com""标题""内容""your mail password""附件路径 没有的可为 空");
5             }

 

 

方法 Class1.Test

 1         /// <summary>
 2         /// 发送邮件类
 3         /// </summary>
 4         /// using System.Net.Mail;
 5         /// using System.Net;
 6         /// <param name="MailFrom">发件人邮箱</param>
 7         /// <param name="MailTo">收件人邮箱</param>
 8         /// <param name="UserName">收件人名称 可为空</param>
 9         /// <param name="MailServer">邮件服务器</param>
10         /// <param name="Subject">标题</param>
11         /// <param name="BodyText">内容</param>
12         /// <param name="Password">发件人密码</param>
13         /// <param name="AttachmentPath">附件地址 可为空</param>
14         /// <returns>是否操作成功</returns>
15         public static bool Test(string MailFrom, string MailTo, string UserName, string MailServer, string Subject, string BodyText, string Password, string AttachmentPath)
16         {
17             MailMessage mail = new MailMessage();
18 
19             // 设置email的'from'和'to'的地址 
20             mail.From = new MailAddress(MailFrom, "发件人名称", Encoding.UTF8);
21             mail.To.Add(new MailAddress(MailTo, UserName, Encoding.UTF8));
22             mail.Subject = Subject;
23             mail.Body = BodyText;
24 
25             // 可选: 使用html格式的Email 
26             mail.IsBodyHtml = true;
27 
28             // 可选: 设置邮件的优先级别为高 
29             mail.Priority = MailPriority.High;
30 
31             // 可选: 附件 
32             if (AttachmentPath.Trim() != "")
33             {
34                 // 注意这里我们创建了一个MailAttachment对象来附加一个文件到email。
35                 mail.Attachments.Add(new Attachment(AttachmentPath.Trim()));
36             }
37 
38             // 使用SmtpClient对象来发送邮件。 
39             SmtpClient smtp = new SmtpClient();
40             smtp.Credentials = new NetworkCredential(MailFrom, Password);   //发信人邮箱、密码
41             smtp.Port = 25;             //端口号
42             smtp.EnableSsl = false;     //是否使用安全套接字层(SSL)加密连接
43             smtp.Host = MailServer;     //发件者的smtp服务器,如smtp.163.com。如果使用本机的邮件服务器,可以设成127.0.0.1
44 
45             try
46             {
47                 smtp.Send(mail);
48                 return true;
49             }
50             catch
51             {
52                 return false;
53             }
54         }

 

不要忘记要要引用:

using System.Net;
using System.Net.Mail;

 

 

环境:vs2005
代码下载:http://files.cnblogs.com/saifang/发邮件的小项目.rar

 

抱歉!评论已关闭.