Saturday, July 21, 2012

Gửi thư trong ASP.NET use C #

Lớp used to send email to 1 or more user, có allow Đình kem tep tin to email ...

Failed to send email is 1 job normal date and write ra 1 program send thư real simple but not ai also known. Minh xin Giới Thiệu with you lớp Email this.

bằng cách sử dụng hệ thống;
bằng cách sử dụng System.Data;
sử dụng System.Configuration;
bằng cách sử dụng System.Web;
sử dụng System.Web.Security;
bằng cách sử dụng System.Web.UI;
sử dụng System.Web.UI.WebControls,
sử dụng System.Web.UI.WebControls. WebParts
sử dụng System.Web.UI.HtmlControls;
bằng cách sử dụng System.Net.Mail; EmailClass không gian tên { public class Email { công cộng chuỗi Send_Email (string SendFrom, chuỗi SendTo, chuỗi Subject, Body chuỗi) { thử { System.Text.RegularExpressions. regex regex = new System.Text.RegularExpressions.Regex (@ "\ w + ([- + '.] \ w +) * @ \ w + ([.] \ w +) * \ \ w + ([-] \ w + ) * "); bool result = Regex.IsMatch (to), nếu (kết quả == false) { trở lại "address email is not valid"; } khác { System.Net.Mail.SmtpClient smtp = new SmtpClient () System.Net.Mail.MailMessage msg = new MailMessage (SendFrom, Send To, Subject, Body); msg.IsBodyHtml = true; smtp.Host = "smtp.gmail.com" ;/ / Use SMTP of the gmail smtp.Send (msg); trở lại "Email has been sent to:" "." + SendTo +; } } catch { return ""; } } công chuỗi Send_Email_With_Attachment (: SendTo chuỗi, chuỗi SendFrom, chuỗi Subject, Body chuỗi, chuỗi AttachmentPath)
{
thử
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex (@ "\ w + ([- + \ w +) * @ \ w + ([-] \ w +) * \ \ w + ([-] \ w +) * ").;

chuỗi từ = SendFrom;
string = SendTo;
chuỗi chủ đề = Tiêu đề;
chuỗi cơ thể = cơ thể;

bool kết quả = Regex.IsMatch (để);

nếu (kết quả == false)
{
trở lại "address email is not valid.";
}
khác
{
thử
{
MailMessage em = new MailMessage (từ, chủ đề, cơ thể);
đính kèm đính kèm = đính kèm (AttachmentPath);

em.Attachments.Add (đính kèm);
em.Bcc.Add (từ);
System.Net.Mail.SmtpClient smtp = new SmtpClient ();
smtp.Host = "smtp.gmail.com" ;/ / For example process using SMTP of the gmail
smtp.Send (em);
return "";
}
catch (Exception ex)
{
trở lại ex.Message;
}
}
}

catch (Exception ex)
{
trở lại ex.Message;
}
}

công chuỗi Send_Email_With_BCC_Attachment (: SendTo chuỗi, chuỗi SendBCC, SendFrom chuỗi, chuỗi Subject, Body chuỗi, chuỗi AttachmentPath)
{
thử
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex (@ "\ w + ([- + '] \ w +) * @ \ w + ([-] \ w +) * \ \ w + ([-] \ w +) * ");....
chuỗi = SendFrom;
chuỗi = SendTo; / / Danh list email is short way nhau bởi mark ","
chuỗi chủ đề = Tiêu đề;
chuỗi cơ thể = cơ thể;
chuỗi bcc = SendBCC;

bool kết quả = true;
String [] ALL_EMAILS = to.Split (';');

foreach (string EmailAddress trong ALL_EMAILS)
{
kết quả = Regex.IsMatch (EmailAddress),
nếu (kết quả == false)
{
trở lại "address email is not valid.";
}
}

nếu (kết quả == true)
{
thử
{
MailMessage em = new MailMessage (từ, chủ đề, cơ thể);
đính kèm đính kèm = đính kèm (AttachmentPath);
em.Attachments.Add (đính kèm);
em.Bcc.Add (bcc ), System.Net.Mail.SmtpClient smtp = new SmtpClient (); smtp.Host = "smtp.gmail.com" ;/ / For example process of the use SMTP gmail smtp.Send (em); return ""; } bắt (Exception ex) { trở lại ex.Message; } } khác { return ""; } } catch (Exception ex) { trở lại ex.Message; } } } }

No comments: