Gửi email khi có bình luận mới được thêm

lập trình


CHÀO!

Tôi đã tìm thấy đoạn mã này trên internet:

http://hozefam.com/post/2012/08/21/Code-snippet-to-send-Email-from-C-code.aspx[^]

Tôi có thể thay đổi mã này theo bất kỳ cách nào để khi người dùng để lại nhận xét về một bài đăng cụ thể, tôi sẽ nhận được email có chủ đề đã viết nhận xét về chủ đề đó không?

Và có lẽ một hướng dẫn có thể chỉ cho tôi cách làm?

/Tina

Giải pháp 1

Giả sử, bạn cung cấp cho người dùng 2 trường, chủ đề (hộp văn bản1) và nhận xét (hộp văn bản2), một nút để gửi nhận xét. Khi nhấp vào sự kiện của nút đó, bạn có thể lấy các giá trị của hộp văn bản1 và hộp văn bản2, đính kèm các giá trị đó vào nội dung thư của bạn, gửi nó đến địa chỉ email được yêu cầu, thế là xong.

Giải pháp 2

Tôi đã chơi với mã của bạn tối nay nhưng thực sự không thể làm cho nó hoạt động chính xác.

Tôi nghĩ là tôi hiểu mã nhưng tôi không chắc lắm vì còn nhiều điều tôi chưa học được. Nhưng bạn có một tệp CS tên là clsMail.cs, tệp này dùng để kiểm soát những gì xảy ra khi gửi email.

Và trong chuỗi SQL, bạn lấy thông tin của mình khi người dùng nhập vào hộp nhận xét, dán vào cơ sở dữ liệu rồi gửi email.

Nhưng tôi không biết liệu mình có làm đúng khi thêm nó vào mã của mình hay không.

Nhưng đây là codebehind của tôi:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
public partial class VisBlognyhed : System.Web.UI.Page
{
    private int kommentar_ID = 1;
    private int indlaeg_ID = 1;
    private int myindent = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        recaptcha.Validate();
        if (Request.QueryString["indlaeg_ID"] != null)
        {
            int q = Convert.ToInt32(Request.QueryString["indlaeg_ID"]);
            //hentProdukt(q);
        }

    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        Image img = e.Item.FindControl("imgBlog") as Image;
        if (img is Image)
        {

            img.Visible = (img.ImageUrl != "");  // will set image visible to false if ImageUrl is empty string
        }
       

    }
//   
    protected void Button_kommentar_Click(object sender, EventArgs e)
    {
        int mParentId = kommentar_ID;
        int mArticleId = indlaeg_ID;

        string mUserName = "quartz";
        string mUserDato = "quartz@msn.com";
        string mDescription = "Test Description";
        int mIndent = myindent;
        

        if (Page.IsValid)
        {
            mUserName = TextBox_navn.Text;
            mUserDato = TextBox_dato.Text;
            mDescription = TextBox_kommentar.Text;
            


            Label_kommentar.Text = "Korrekt";
            Label_kommentar.ForeColor = System.Drawing.Color.Green;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES(@dato, @navn, @kommentar, @indlaeg)";
            cmd.Parameters.Add("@dato", SqlDbType.DateTime).Value = TextBox_dato.Text;
            cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
            cmd.Parameters.Add("@kommentar", SqlDbType.Text).Value = TextBox_kommentar.Text;
            cmd.Parameters.Add("@indlaeg", SqlDbType.Int).Value = Request.QueryString["indlaeg_ID"];
            
cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES('" +mParentId + "','" + mArticleId +  "','" + mUserName +  "','" + mUserDato +  "','" + mDescription + "','" + "')";

            




            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            TextBox_dato.Text = "";
            TextBox_navn.Text = "";
            TextBox_kommentar.Text = "";


            Label_kommentar.Text = "Din kommentar er modtaget.";


        }
        else
        {
            Label_kommentar.Text = "Du skal indtaste Captcha eller du har indtastet forkert";
            Label_kommentar.ForeColor = System.Drawing.Color.Red;
        }



            

        
    }
}

Và mã cho Lớp:

C#
using System;
using System.Web.Mail;
namespace JumpyForum
{
/// <summary>
/// Summary description for clsMail
/// </summary>
public class clsMail
{
	public bool SendMail(string ToM, string FromM, string CcM, string MSubject, string MBody ) // Opens database connection with Granth in SQL SERVER
		{
			try
			{
				MailMessage objMM = new MailMessage();
				//'Set the properties
				objMM.To = ToM;//"razesh@hotmail.com";
				objMM.From = FromM;//"connectrajesh@hotmail.com";

				//'If you want to CC this email to someone else...
				objMM.Cc = CcM;//"flytorajesh@someaddress.com";

				//'If you want to BCC this email to someone else...
				//objMM.Bcc = "studyrajesh@hotmail.com";

				//'Send the email in text format
				objMM.BodyFormat = MailFormat.Html ;

				//'(to send HTML format, change MailFormat.Text to MailFormat.Html)

				//'Set the priority - options are High, Low, and Normal

				objMM.Priority = MailPriority.Normal;

				//'Set the subject
				objMM.Subject = MSubject;//"Hello there testing!";

				//'Set the body - use VbCrLf to insert a carriage return
				objMM.Body = MBody;//"Hi! How are you doing?";
				SmtpMail.SmtpServer = "localhost"; 
				SmtpMail.Send(objMM);

				return true;
			}
			catch
			{
				return false;
			}
			finally
			{
				
			}

		}
	}
}

Và trong MailPriority này có lỗi trên

Mong bạn có thể hướng dẫn thêm cho mình

コメント

タイトルとURLをコピーしました