好久没写博客了,今天闲着没事在家做了个简单的asp.net的内置对象Mail的例子,和大家分享下。欢迎大家交流,给出建议。谢谢!
主要界面:
前台aspx代码:
View Code
1 <div style="500px; height:20px;margin-bottom:15px;">
2 <p align="center"><font face="黑体" size="3"><span style="letter-spacing: 3pt; color: #b12213;font-size: 16px;"><br /><br />邮件发送</span></font></p>
3 </div>
4 <table style="100%; margin-top:50px;">
5 <tr>
6 <td style="10%"></td>
7 <td>
8 用户名 :<asp:TextBox ID="txtUserName"
9 runat="server"></asp:TextBox>@
10 <asp:DropDownList ID="dlistSmtp" runat="server">
11 <asp:ListItem>163.com</asp:ListItem>
12 <asp:ListItem>sina.com</asp:ListItem>
13 <asp:ListItem>qq.com</asp:ListItem>
14 </asp:DropDownList> 服务器选择
15 <asp:Label ID="Label2" runat="server" ForeColor="Red" Text="*必填"></asp:Label>
16 <br />
17 登录密码 :<asp:TextBox ID="txtPassword" runat="server"
18 TextMode="Password"></asp:TextBox>
19 <asp:Label ID="Label3" runat="server" ForeColor="Red" Text="*必填"></asp:Label>
20 <br />
21 收信人邮箱:<asp:TextBox ID="txtTo"
22 runat="server"></asp:TextBox>
23 <asp:Label ID="Label4" runat="server" ForeColor="Red" Text="*必填"></asp:Label>
24 <br />
25 主题:
26 <br />
27 <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox><br />
28 正文:邮件内容不宜超过200字!<br />
29 <textarea id="txtContent" style="80%" runat="server" cols="20" rows="3"></textarea>
30 <br />
31 添加附件:
32
33 <asp:FileUpload ID="FileUpload1" runat="server" />
34 <asp:Button ID="btnAddFile" runat="server" onclick="btnAddFile_Click" Text="添加附件" />
35 <br />
36 已添加附件:<asp:Label ID="lbl_showAttach" runat="server" ForeColor="Blue"></asp:Label>
37 <br />
38 <br />
39
40 <asp:Button ID="btnSendEmail" runat="server" onclick="btnSendEmail_Click"
41 Text="邮件发送" style="height: 21px" />
42
43 <asp:Button ID="btnExitEmail" runat="server" onclick="btnExitEmail_Click"
44 Text="重新填写" />
45 </td>
46 </tr>
47 </table>
后台.cs文件代码:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mail;//引用mail的命名空间
6 using System.Text.RegularExpressions;//提供Regex类,表示不可变的正则表达式。
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9
10 public partial class _Default : System.Web.UI.Page
11 {
12 public static string Password = "";
13 public static bool First = true;
14 protected void Page_Load(object sender, EventArgs e)
15 {
16
17 }
18
19 protected void btnSendEmail_Click(object sender, EventArgs e) //发送邮件
20 {
21 MailMessage myMail = new MailMessage(); //新建一个mail的对象
22 myMail.To = this.txtTo.Text; //收件人邮箱地址
23 myMail.From = this.txtUserName.Text+"@"+this.dlistSmtp.Text;//发件人邮箱地址
24 myMail.Subject = this.txtTitle.Text; //邮件主题
25 myMail.Body = this.txtContent.Value; //邮件正文
26 myMail.BodyFormat = MailFormat.Html; //邮件格式
27 myMail.Priority = MailPriority.High; // 设置为高级优先权
28 myMail.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312"); //设置邮件编码类型
29 SmtpMail.SmtpServer = "smtp."+this.dlistSmtp.SelectedValue.ToString();//服务器名称
30 if (this.lbl_showAttach.Text != "")
31 {
32 this.lbl_showAttach.Text = this.lbl_showAttach.Text.Remove(this.lbl_showAttach.Text.LastIndexOf(";"), 1);//去掉最后一个分号
33 string[] attachs = Regex.Split(this.lbl_showAttach.Text, ";"); //字符串按分号分隔存储在数组中
34 for (int i = 0; i < attachs.Length - 1; i++)
35 {
36 //建立附件对象实例,保存在服务器上
37 MailAttachment ma = new MailAttachment(Server.MapPath("upAttach\\") + attachs[i]);
38 //添加附件
39 myMail.Attachments.Add(ma);
40 }
41 }
42
43
44 myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
45 myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.txtUserName.Text.Trim());
46 myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Password);
47 try
48 {
49 SmtpMail.Send(myMail);
50 Response.Write("<script>alert('邮件发送成功!');</script>");
51 }
52 catch
53 {
54 Response.Write("<script>alert('邮件发送失败!');</script>");
55 }
56 finally
57 {
58 this.txtTitle.Text = "";
59 this.txtContent.Value = "";
60 this.lbl_showAttach.Text = "";
61 }
62 }
63
64 protected void btnExitEmail_Click(object sender, EventArgs e) //取消发送
65 {
66 this.txtTitle.Text = "";
67 this.txtContent.Value = "";
68 this.lbl_showAttach.Text = "";
69 }
70
71 protected void btnAddFile_Click(object sender, EventArgs e) //添加附加
72 {
73 if (First == true)
74 {
75 Password = this.txtPassword.Text;
76 }
77 //获得上传文件的完全名称
78 string fullFilename = this.FileUpload1.PostedFile.FileName;
79 //获得上传文件的文件名+1表示匹配到最后一个字符
80 string fileName = fullFilename.Substring(fullFilename.LastIndexOf("\\") + 1);
81 this.FileUpload1.SaveAs(Server.MapPath("upAttach\\") + fileName);
82 this.lbl_showAttach.Text += fileName + ";";
83 First = false;
84 }
85 }
其中SMTP是简单邮件传输协议(Simple Mail Transfer Protocol)的简称,是您发送Email所必须用到的。默认端口是25,
常用的免费SMTP服务器如下,用户名和密码就是您Email的帐号对应
邮箱 | 对应的免费SMTP服务器 | |
---|---|---|
01 | @163.com | smtp.163.com |
02 | @126.com | smtp.126.com |
03 | Sina免费邮件 | smtp.sina.com.cn |
04 | Gmail | smtp.gmail.com (SSL) |
05 | @sohu.com | smtp.sohu.com |
06 | 21cn邮箱 | smtp.21cn.com |
07 | Yahoo中国免费邮件 | smtp.mail.yahoo.com.cn smtp.yahoo.cn |
08 | @hotmail.com/@msn.com | 不支持SMTP |
09 | @qq.com | smtp.qq.com,需要到邮箱里先设置开通 |
10 | 263电子邮局 | smtp.263.net |
我在做的过程中碰到了几个难点和大家分享下:
1.服务器拒绝服务
解决方法:添加发送人登录邮箱,获取服务器请求
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.txtUserName.Text.Trim()); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Password);
2.添加附件的时候,点击添加的按钮,导致上面文本框中的密码清空,使1中的值不能正常
解决方法:在初始的时候设置全局静态变量,在添加附件前保存密码的值(最笨的办法了,有好的方法欢迎交流)
public static string Password = ""; public static bool First = true;
if (First == true) { Password = this.txtPassword.Text; } //首次添加附件的时候改变bool的值
这样基本问题就解决了,还是挺简单的。我试过新浪和QQ邮箱,都是可以的。但是QQ的好像