ASP.Net 2.0 发送邮件的代码
ASP.Net 2.0 发送邮件的代码
这两天因为要做一个可以收集用户提交信息,然后发送邮件到指定邮箱的网页。在 asp.net 2.0 里面发送邮件可以使用 System.Net.Mail 下面的类。http://www.systemnetmail.com/faq/2.1.aspx 这个地址有 System.Net.Mail 的作者写的仿佛是 MSDN 类似的 Faq,看代码的时候可以参考以下。什么都不说了,下面贴出来代码吧,毕竟代码是最重要的:
1protected void btnSend_Click(object sender, EventArgs e)
2 {
3 MailMessage mailMessage = new MailMessage();
4 string strBody;
5
6 mailMessage.From = new MailAddress("365rss@gmail.com");
7 mailMessage.To.Add("prlibertine@gmail.com");
8 mailMessage.Subject = "365Rss.CN ";
9
10 strBody = "<h2>" + ddlMainTag.SelectedItem.Text + "</h2><br>";
11 strBody += "<h4>" + tbTag.Text + "</h4><br><hr><br>";
12 strBody += "·" + tbAdv.Text;
13
14 sendMail(mailMessage);
15 }
16
17 //SendMail
18 private void sendMail(MailMessage mail)
19 {
20 SmtpClient smtpClient = new SmtpClient();
21 smtpClient.EnableSsl = true;
22 smtpClient.Host = "smtp.gmail.com";
23 smtpClient.Port = 465;
24 smtpClient.Credentials = new NetworkCredential("365rss@gmail.com", "pwd");
25 try
26 {
27 smtpClient.Send(mail);
28 Label1.Text = "ok";
29 }
30 catch
31 {
32 Label1.Text = "false";
33 }
34 }
35
2 {
3 MailMessage mailMessage = new MailMessage();
4 string strBody;
5
6 mailMessage.From = new MailAddress("365rss@gmail.com");
7 mailMessage.To.Add("prlibertine@gmail.com");
8 mailMessage.Subject = "365Rss.CN ";
9
10 strBody = "<h2>" + ddlMainTag.SelectedItem.Text + "</h2><br>";
11 strBody += "<h4>" + tbTag.Text + "</h4><br><hr><br>";
12 strBody += "·" + tbAdv.Text;
13
14 sendMail(mailMessage);
15 }
16
17 //SendMail
18 private void sendMail(MailMessage mail)
19 {
20 SmtpClient smtpClient = new SmtpClient();
21 smtpClient.EnableSsl = true;
22 smtpClient.Host = "smtp.gmail.com";
23 smtpClient.Port = 465;
24 smtpClient.Credentials = new NetworkCredential("365rss@gmail.com", "pwd");
25 try
26 {
27 smtpClient.Send(mail);
28 Label1.Text = "ok";
29 }
30 catch
31 {
32 Label1.Text = "false";
33 }
34 }
35