Merhaba arkadaşlar bu örnek dersimizde asp.net bir web sayfada mail send işlemi nasıl oluşturulur görelim
ilk önce tablo yapımızı oluşturalım örnek bir yapı ben oluşturdum sizlerde resimdeki şablona göre bir tasarım yaparsınız
Gelelim şimdi Code Behind Tarafına Kaynak kodumuzu bu şekilde yazıyoruz
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Windows;
using System.Windows.Forms;
namespace SendMail
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsend_Click(object sender, EventArgs e)
{
//MailMessage message = new MailMessage();
//message.To.Add(txtto.Text);
////example adres mail@mail.com
//message.From = new MailAddress(txtfrom.Text);
//message.Subject = txtsubject.Text;
//message.Body = txtmessage.Text;
if (rdhtml.Checked == true)
{
Html();
MessageBox.Show(“Mail Gönderildi”);
CleanTable();
}
else if (rdmetin.Checked == true)
{
Metin();
MessageBox.Show(“Mail Gönderildi”);
CleanTable();
}
}
protected void rdhtml_CheckedChanged(object sender, EventArgs e)
{
Html();
}
void Html()
{
try
{
MailMessage message = new MailMessage();
message.To.Add(txtto.Text);
//example adres mail@mail.com
message.From = new MailAddress(txtfrom.Text);
message.Subject = txtsubject.Text;
message.Body = txtmessage.Text;
message.IsBodyHtml = true;
//Attach file using FileUpload Control and put the file in memory stream
if (ContentFile.HasFile)
{
message.Attachments.Add(new Attachment(ContentFile.PostedFile.InputStream, ContentFile.FileName));
SmtpClient smtp = new SmtpClient();
// smtp.Host = “smtp.gmail.com”; //Or Your SMTP Server Address
smtp.Host = txtsmtpserver.Text;
smtp.Credentials = new System.Net.NetworkCredential(txtusername.Text, txtpass.Text);
//”YourGmailID@gmail.com”, “YourGmailPassword”);
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(message);
}
}
catch (Exception ex)
{
Response.Write(“</br> ” + ex.Message);
}
}
void Metin()
{
try
{
MailMessage message = new MailMessage();
message.To.Add(txtto.Text);
//example adres mail@mail.com
message.From = new MailAddress(txtfrom.Text);
message.Subject = txtsubject.Text;
message.Body = txtmessage.Text;
message.IsBodyHtml = false;
//Attach file using FileUpload Control and put the file in memory stream
if (ContentFile.HasFile)
{
message.Attachments.Add(new Attachment(ContentFile.PostedFile.InputStream, ContentFile.FileName));
SmtpClient smtp = new SmtpClient();
// smtp.Host = “smtp.gmail.com”; //Or Your SMTP Server Address
smtp.Host = txtsmtpserver.Text;
smtp.Credentials = new System.Net.NetworkCredential(txtusername.Text, txtpass.Text);
//”YourGmailID@gmail.com”, “YourGmailPassword”);
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(message);
}
}
catch (Exception ex)
{
Response.Write(“</br> ” + ex.Message);
}
}
protected void rdmetin_CheckedChanged(object sender, EventArgs e)
{
Metin();
}
void CleanTable()
{
txtfrom.Text = “”;
txtmessage.Text = “”;
txtpass.Text = “”;
txtsmtpserver.Text = “”;
txtsubject.Text = “”;
txtto.Text = “”;
txtusername.Text = “”;
}
}
}