Code gửi mail và demo trong asp.net với các tài khoản gmail, yahoo ....

Bài viết sau sẽ giới thiệu code gửi mail với nhiều tùy chọn cho phép bạn sử dụng với các tài khoản gmail, yahoo, domain (hoclaptrinhweb.com) .... bạn có thể xem demo tại đây : hoclaptrinhweb.com/code/sendmail.aspx


Với gmail thì có thể dùng các port sau : 25, 465 và 587
Với yahoo thì có thể dùng các port sau : 25, 465 và 587
Với domain thông thường thì port là : 25

Và sau đây là đoạn code C# các bạn có thể chuyển sang VB.NET

Mã:
[COLOR=#141414][FONT=Consolas] public [/FONT][/COLOR][COLOR=#993333][FONT=Consolas]static[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] string SendMailFull[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]string mailFrom[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]string mailpass[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]string host[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]string port[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]string mailTo[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] string subject[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] string content[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] bool enableSsl[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]{[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]        try[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]{[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]            var msg [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] new MailMessage[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]{[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]                IsBodyHtml [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#000000][FONT=Consolas][B]true[/B][/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]                Body [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] content[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]                From [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] new MailAddress[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]mailFrom[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] mailFrom[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]}[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]            msg.[/FONT][/COLOR][COLOR=#202020][FONT=Consolas]To[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].[/FONT][/COLOR][COLOR=#202020][FONT=Consolas]Add[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]new MailAddress[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]mailTo[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]            msg.[/FONT][/COLOR][COLOR=#202020][FONT=Consolas]Subject[/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] subject[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]

[COLOR=#141414][FONT=Consolas]            var client [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] new SmtpClient[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]host[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#993333][FONT=Consolas]int[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].[/FONT][/COLOR][COLOR=#202020][FONT=Consolas]Parse[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]port[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]{[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]                Credentials [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]                    new NetworkCredential[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]mailFrom[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] mailpass[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR][COLOR=#339933][FONT=Consolas],[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]                EnableSsl [/FONT][/COLOR][COLOR=#339933][FONT=Consolas]=[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] enableSsl[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]}[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]

[COLOR=#141414][FONT=Consolas]            client.[/FONT][/COLOR][COLOR=#202020][FONT=Consolas]Send[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]msg[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]

[COLOR=#B1B100][FONT=Consolas]return[/FONT][/COLOR][COLOR=#FF0000][FONT=Consolas]""[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]}[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]        catch [/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Exception ex[/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]{[/FONT][/COLOR]
[COLOR=#B1B100][FONT=Consolas]return[/FONT][/COLOR][COLOR=#141414][FONT=Consolas] ex.[/FONT][/COLOR][COLOR=#202020][FONT=Consolas]ToString[/FONT][/COLOR][COLOR=#009900][FONT=Consolas]([/FONT][/COLOR][COLOR=#009900][FONT=Consolas])[/FONT][/COLOR][COLOR=#339933][FONT=Consolas];[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]}[/FONT][/COLOR]
[COLOR=#009900][FONT=Consolas]}[/FONT][/COLOR][COLOR=#141414][FONT=Georgia]
[/FONT][/COLOR]


Với chức năng gửi email này các bạn có thể áp dụng vào các trường hợp như :
  1. Gửi liên hệ cho admin (khi người dùng gửi liên hệ) (ví dụ : hoclaptrinhweb.com/contact.aspx)
  2. Gửi mail thông báo khi hoàn thành mua hàng
  3. Kích hoạt mail khi đăng ký tài khoản , đổi mật khẩu
  4. Và rất nhiều cái áp dựng khác ............
 
Top