İçeriğe geç

ASP.NET ile Gmail üzerinden mail gönderme

5.628 defa okundu

Konu: Programlama

ASP.NET MVC ile Gmail üzerinden mail göndermek için aşağıdaki void kullanılabilir. Burada mail gönderen gmail hesabı deneme@gmail.com dır. deneme2@gmail.com ise mail gönderilen mail adresi bu adres dinamik olarak belirlenebilir.

MVC kullandığım için Model ve Control yapısı aşağıdaki gibidir. Ayrıca veri tabanı işlemini dapper kullanarak yaptım. Bu örnekte web sayfanızdaki iletişim formu doldurulduktan sonra size mesaj veri tabanına kayıt edildikten sonra ayrıca bilgi olarak mail adresinize mail gönderilmektedir.

Gmail ayarlarından bir uygulamanın mail göndermesine izin verilmek için aşağıdaki linkteki yazıyı okuyabilirsiniz.

GMail üzerinden mail gönderme İzni verme

Model

C#
  1. namespace Deneme.Models
  2. {
  3. public class Contact
  4. {
  5. public int Id { get; set; }
  6. public Nullable<System.DateTime> Date { get; set; }
  7. public string Name { get; set; }
  8. public string Email { get; set; }
  9. public string Phone { get; set; }
  10. public string Subject { get; set; }
  11. public string Message { get; set; }
  12. }
  13. }
  14. Control
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Web;
  19. using System.Web.Mvc;
  20. using Deneme.Models;
  21. using Dapper;
  22. using System.Net.Mail;
  23. namespace Deneme.Controllers
  24. {
  25. public class IletisimController : Controller
  26. {
  27. public ActionResult Index()
  28. {
  29. ViewBag.Error = 0;
  30. return View();
  31. [HttpPost]
  32. [ValidateAntiForgeryToken]
  33. public ActionResult Index(Contact contact)
  34. {
  35. if (ModelState.IsValid)
  36. {
  37. using (var connection = DBContext.GetOpenConnection())
  38. {
  39. contact.Date = DateTime.Now;
  40. var sqlQuery = @"INSERT INTO Contact(Name,EMail,Phone,Subject,Message,Date)
  41. VALUES (@Name,@EMail,@Phone,@Subject,@Message,@Date)";
  42. connection.Execute(sqlQuery, contact);
  43. ViewBag.Error = 1;
  44. this.MailGonder(contact);
  45. }
  46. return View(contact);
  47. }
  48. else
  49. {
  50. ViewBag.Error = 2;
  51. }
  52. return View();
  53. }
  54. public void MailGonder(Contact contact)
  55. {
  56. MailMessage loginInfo = new MailMessage();
  57. if (contact.Email != null)
  58. {
  59. loginInfo.To.Add("deneme2@gmail.com");
  60. loginInfo.From = new MailAddress("deneme@gmail.com");
  61. loginInfo.Subject = contact.Subject;
  62. loginInfo.Body = contact.Message + "<br/>" + "Mail Gönderenin Adı Soyadı:" + contact.Name + "<br/>" + "Mail Gönderenin Telefon Numarası:" + contact.Phone + "<br/>" + "Mail Gönderenin Mail Adresi:" + contact.Email;
  63. loginInfo.IsBodyHtml = true;
  64. SmtpClient smtp = new SmtpClient();
  65. smtp.Host = "smtp.gmail.com";
  66. smtp.Port = 587;
  67. smtp.EnableSsl = true;
  68. smtp.Credentials = new System.Net.NetworkCredential("deneme@gmail.com", "Şifre");
  69. smtp.Send(loginInfo);
  70. }
  71. }
  72. }
  73. }

Bu yazı işinize yaradı mı?

Programlama konusundaki diğer yazılar

Programlama: 12 yazının tümü →

WhatsApp'ta paylaş

İlgili Yazılar