27 Ağustos 2011 Cumartesi

ASP.NET ile Mail Gönderme




Bu makalemizde ASP.NET ve C# ile SMTP server üzerinden mail gönderme kodlarınız yazacağız.
Öncelikle yeni bir ASP sayfası açıyoruz istediğiniz dizaynı yaptıktan sonra forma 3 tane textBox, 1 tane Label ve 1 tane de buton oluşturuyoruz,  TextBox’ un adları : ” txtKonu, txtMesaj ve txtEmail”. Label’in  adını txtSonuc yapıyoruz. txtKonu’ya oluşturulan formadan konuyu giriyoruz, txtMesaj’ a  gönderilecek mesajı yazıyoruz, txtEmail’ e ise gönderen kişinin mail adresi yazılacak. txtSonuc label’ine de maili gönder butonuna bastıktan sonra hata var ise  catch yapısından dönen sonuç görüntülenecek. Aşağıdaki kodu oluşturduğunuz butonun click event’ ine yapıştıryoruz. Bu kodlar % 100 çalışmaktadır.
Kolay gelsin.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGonder_Click(object sender, EventArgs e)
{
try
{
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(“smtp mail sunucunuz”);
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = false;
smtp.Port = 587;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential(“mail nereye gidecek”, “gidecek mail şifresi”);
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
smtp.Credentials = cred;
mail.Priority = MailPriority.High;
mail.To.Add(“mail gidecek diğer adres”);//isteğe bağlı
mail.To.Add(“mail gidecek diğer adres 2″);//isteğe bağlı
mail.From = new System.Net.Mail.MailAddress(“mail nereye gidecek”);
mail.Subject = txtKonu.Text;
mail.Body = (txtMesaj.Text + ” gönderen kişinin e-maili:” + txtEmail.Text);
mail.IsBodyHtml = false;
smtp.Send(mail);
}
catch (Exception ex)
{
txtSonuc.Text = ex.Message;
}
}
}

Yayınlayan : Mansur İşçel

Hiç yorum yok:

Yorum Gönder