none
ASP.NET Send Mail FAQ: วิธีการส่ง email

คำตอบ



  • ในการส่ง mail เราจะใช้ ‘System.Net.Mail’



    Code-Behind:

    Using System.Net.Mail;

    public void SendEmail(string from, string to, string bcc, string cc, string subject, string body)
    {
       
    try
       
    {   
           
    MailMessage NewEmail = new MailMessage();
           
    NewEmail.From = new MailAddress(from);
           
    NewEmail.To.Add(new MailAddress(to));
           
    if (!String.IsNullOrEmpty(bcc))
           
    { NewEmail.Bcc.Add(new MailAddress(bcc));
           
    }
           
    if (!String.IsNullOrEmpty(cc))
           
    {
               
    NewEmail.CC.Add(new MailAddress(cc));
           
    }
           
    NewEmail.Subject = subject;
           
    NewEmail.Body = body;
           
    NewEmail.IsBodyHtml = true;
           
    SmtpClient mSmtpClient = new SmtpClient();
           
    // Send the mail message
            mSmtpClient
    .Send(NewEmail);
           
    this.Label1.Text = "Email sent successful.";
       
    }
       
    catch
       
    { 
           
    this.Label1.Text = "Email sent failed"; 
       
    }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
       
    string from = "sender address";// like username@server.com
       
    string to = TextBox1.Text; //Recipient address.
       
    string bcc = "";
       
    string cc = "";
       
    string subject = "text";
       
    string body = "text";
       
    SendEmail(from, to, bcc, cc, subject, body); 
    }

    Web.config:

    <system.net>
     
    <mailSettings>
       
    <smtp>
         
    <networkhost="your stmp server"port="25"userName="your from email"password="your password"/>
       
    </smtp>
     
    </mailSettings>
    </system.net>



    ฟอรัมที่เกี่ยวข้อง

    http://forums.asp.net/t/1207868.aspx




    Supa Sethasiripong [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • แก้ไขโดย supa_sModerator 23 มีนาคม 2555 8:12
    • ทำเครื่องหมายเป็นคำตอบโดย supa_sModerator 26 มีนาคม 2555 2:22
    23 มีนาคม 2555 8:11