Cấp bậc tác giả:

DOTNET

Send mail trong ASP.NET

Được viết bởi QuangIT ngày 03/08/2012 lúc 04:46 PM
Việc gửi mail là 1 công việc thường ngày và viết ra 1 chương trình gửi mail thật đơn giản nhưng không phải ai cũng biết. Mình xin giới thiệu với các bạn class Email này.
  • 0
  • 10047
Tải tệp tin: Click ở đây

Send mail trong ASP.NET

Việc gửi mail là 1 công việc thường ngày và viết ra 1 chương trình gửi mail thật đơn giản nhưng không phải ai cũng biết. Mình xin giới thiệu với các bạn class Email này.
View Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

namespace EmailClass
{
public class Email
{
public string Send_Email(string SendFrom,string SendTo, string Subject, stringBody)

try
{
System.Text.RegularExpressions.Regex regex = newSystem.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");


bool result = regex.IsMatch(to);
if (result == false)
{
return "Địa chỉ email không hợp lệ.";
}
else
{
System.Net.Mail.SmtpClient smtp = new SmtpClient();
System.Net.Mail.MailMessage msg = new MailMessage(SendFrom,SendTo,Subject,Body);
msg.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";//Sử dụng SMTP của gmail
smtp.Send(msg);
return "Email đã được gửi đến: " + SendTo + ".";
}
}
catch
{
return "";
}
}

public string Send_Email_With_Attachment(string SendTo, string SendFrom, stringSubject, string Body, string AttachmentPath)
{
try
{
System.Text.RegularExpressions.Regex regex = newSystem.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

string from = SendFrom;
string to = SendTo;
string subject = Subject;
string body = Body;

bool result = regex.IsMatch(to);

if (result == false)
{
return "Địa chỉ email không hợp lệ.";
}
else
{
try
{
MailMessage em = new MailMessage(from, to,subject, body);
Attachment attach = new Attachment(AttachmentPath);

em.Attachments.Add(attach);
em.Bcc.Add(from); 
System.Net.Mail.SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";//Ví dụ xử dụng SMTP của gmail 
smtp.Send(em); 
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
}

catch (Exception ex)
{
return ex.Message;
}
}

public string Send_Email_With_BCC_Attachment(string SendTo, string SendBCC,string SendFrom, string Subject, string Body, string AttachmentPath)

try
{
System.Text.RegularExpressions.Regex regex = newSystem.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); 
string from = SendFrom;
string to = SendTo; //Danh sách email được ngăn cách nhau bởi dấu ";"
string subject = Subject;
string body = Body;
string bcc = SendBCC; 

bool result = true;
String[] ALL_EMAILS = to.Split(';');

foreach (string emailaddress in ALL_EMAILS)
{
result = regex.IsMatch(emailaddress);
if (result == false)
{
return"Địa chỉ email không hợp lệ.";
}
}

if (result == true)
{
try
{
MailMessage em = new MailMessage(from, to, subject, body);
Attachment attach = new Attachment(AttachmentPath);
em.Attachments.Add(attach);
em.Bcc.Add(bcc);

System.Net.Mail.SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";//Ví dụ xử dụng SMTP của gmail
smtp.Send(em);

return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
else
{
return "";
}
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}

Nguồn bài viết: Dngaz.com

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML