c# mvc3 email versturen en ook tegelijk opslaan in db

Status
Niet open voor verdere reacties.

InnerMe

Gebruiker
Lid geworden
10 nov 2009
Berichten
15
Hallo,

Weet iemand hoe je een email vanuit een from in asp.net kan versturen maar ook tegelijk kan opslaan in een db?

Het versturen lukt me nu, en de code voor het opslaan heb ik ook maar ik weet niet hoe ik beide moet combineren.
Ik heb de volgende stukken code:

HTML:
[HttpPost]
        public ActionResult Contact(MailFormModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add("email1@gmail.com");
                    mail.CC.Add("email2@hotmail.com");
                    mail.From = new MailAddress (model.strEmail);
                    mail.Subject = "New Message From: " + model.strName;
                    mail.Body = model.strText;
                    mail.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = ConfigurationManager.AppSettings["SMTP"];
                    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                    mail.Dispose();
                }
                catch (Exception)
                {
                    return View(model);
                }

en voor het opslaan heb ik dit:

HTML:
 SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["GegevensDatabase"].ToString();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO tblContactRequest (strName, strEmail, strText) VALUES (@strName, @strEmail, @strText)";

            cmd.Parameters.Add(new SqlParameter("@strName", model.strName));
            cmd.Parameters.Add(new SqlParameter("@strEmail", model.strEmail));
            cmd.Parameters.Add(new SqlParameter("@strText", model.strText.));

            con.Open();
            cmd.ExecuteReader();
            con.Close();

Weet iemand de oplossing?
 
Roep de functie voor het wegschrijven naar de database op na je de mail hebt verstuurd? (na smtp.send(mail))
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan