主控台程式、WPF、WebForm 及 ASP.NET MVC 我都測試了一下,主控台程式和 WPF 都用了 System.Net.Mail ,WebForm 和 ASP.NET MVC 都可以用 System.Web.Mail ,而 ASP.NET MVC 3 直接用 WebMail 更方便。下面我把代碼分別貼出來。

 

主控台程式:

 

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.Net.Mail;//增加參考
06.
07.namespace SendEmail
08.{
09. class Program
10. {
11. static void Main(string[] args)
12. {
13. Program p = new Program();
14. bool flag = p.SendEmail();
15. if (flag)
16. {
17. Console.Write("Success !");
18. }
19.
20. Console.Read();
21. }
22.
23. public bool SendEmail()
24. {
25. MailMessage msg = new MailMessage();
26. msg.To.Add("1234567@qq.com");//郵件接收者的帳號
27. msg.From = new MailAddress("1234567@qq.com", "nickname", System.Text.Encoding.UTF8);//發送郵件的帳號及顯示名稱和字元編碼
28. msg.Subject = "Subject";//郵件標題
29. msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼
30. msg.Body = "Content";//郵件內容
31. msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼
32. msg.IsBodyHtml = false;//是否是HTML郵件
33. msg.Priority = MailPriority.High;//郵件優先順序
34.
35. SmtpClient client = new SmtpClient();
36. client.Credentials = new System.Net.NetworkCredential("1234567@qq.com", "password");//註冊的郵箱和密碼,就QQ郵箱而言,如果設置了獨立密碼,要用獨立密碼代替密碼
37. client.Host = "smtp.qq.com";//QQ郵箱對應的SMTP伺服器
38. object userState = msg;
39. try
40. {
41. client.SendAsync(msg, userState);
42. return true;
43. }
44. catch (Exception ex)
45. {
46. Console.WriteLine(ex.Message);
47. return false;
48. }
49. }
50. }
51.}



WPF



01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Net.Mail;//增加參考
05.using System.Text;
06.using System.Windows;
07.using System.Windows.Controls;
08.using System.Windows.Data;
09.using System.Windows.Documents;
10.using System.Windows.Input;
11.using System.Windows.Media;
12.using System.Windows.Media.Imaging;
13.using System.Windows.Navigation;
14.using System.Windows.Shapes;
15.
16.namespace WPFSendMail
17.{
18. /// <summary>
19. /// MainWindow.xaml 的交互邏輯
20. /// </summary>
21. public partial class MainWindow : Window
22. {
23. public MainWindow()
24. {
25. InitializeComponent();
26. }
27.
28. private void Button_Click(object sender, RoutedEventArgs e)
29. {
30. bool flag = SendMail();
31. if (flag)
32. {
33. MessageBox.Show("Success !","Tips");
34. }
35. }
36.
37. public bool SendMail()
38. {
39. MailMessage message = new MailMessage();
40. message.To.Add("1234567@qq.com");
41. message.From = new MailAddress("1234567@qq.com","nickname",Encoding.UTF8);
42. message.Subject = "Sending e-mail in WPF !";
43. message.SubjectEncoding = Encoding.UTF8;
44. message.Body = "Awesome";
45. message.BodyEncoding = Encoding.UTF8;
46. message.IsBodyHtml = false;
47. message.Priority = MailPriority.Normal;
48. Attachment att = new Attachment("text.txt");//添加附件,確保路徑正確
49. message.Attachments.Add(att);
50.
51. SmtpClient smtp = new SmtpClient();
52. smtp.Credentials = new System.Net.NetworkCredential("1234567@qq.com","password");
53. smtp.Host = "smtp.qq.com";
54. object userState = message;
55.
56. try
57. {
58. smtp.SendAsync(message,userState);
59. return true;
60. }
61. catch (Exception ex)
62. {
63. MessageBox.Show(ex.Message,"Tips");
64. return false;
65. }
66. }
67. }
68.}



WINFORM



01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.Web;
06.using System.Web.UI;
07.using System.Web.UI.WebControls;
08.using System.Web.Mail;//增加參考
09.
10.namespace WebFormSendEmail
11.{
12. public partial class WebForm1 : System.Web.UI.Page
13. {
14. protected void Page_Load(object sender, EventArgs e)
15. {
16.
17. }
18.
19. protected void Button1_Click(object sender, EventArgs e)
20. {
21. bool flag = SendMail();
22. if (flag)
23. {
24. Response.Write("<script>alert('Success !');</script>");
25. }
26. }
27.
28. public bool SendMail()
29. {
30. MailMessage message = new MailMessage();
31. message.To = "1234567@qq.com";
32. message.From = "1234567@qq.com";
33. message.Subject = "Sending e-mail in Web !";
34. message.Body = "Awesome";
35. //基本許可權
36. message.Fields.Add("HTTP://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
37. //使用者名
38. message.Fields.Add("HTTP://schemas.microsoft.com/cdo/configuration/sendusername", "1234567@qq.com");
39. //密碼
40. message.Fields.Add("HTTP://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
41.
42. SmtpMail.SmtpServer = "smtp.qq.com";
43. try
44. {
45. SmtpMail.Send(message);
46. return true;
47. }
48. catch (Exception ex)
49. {
50. Response.Write(ex.Message);
51. return false;
52. }
53. }
54. }
55.}


ASP.NET MVC
01.try
02. {
03. WebMail.SmtpServer = "smtp.qq.com";
04. WebMail.SmtpPort = 25;//埠號,不同SMTP伺服器可能不同,可以查一下
05. WebMail.EnableSsl = false;//禁用SSL
06. WebMail.UserName = "1234567@qq.com";
07. WebMail.Password = "password";
08. WebMail.From = "1234567@qq.com";
09. WebMail.Send("1234567@qq.com","Subject",「Content");
10. }
11. catch (Exception)
12. {
13.
14. }
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()