
Merhaba arkadaşlar,
Daha önce anlatmış olduğum c# ile email gönderim uygulamasını biraz geliştirdim.
Bu versiyonda ekstra olarak şunları ekledim:
- SSL kontrolü getirildi.
- CC alanı getirildi.
- Attach(Ek) ekleme alanı getirildi.
Formun dizaynı resimdeki gibidir.
Kodları aşağıda görebilirsiniz :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EmailSend
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
string attachFile;
private void buttonSend_Click(object sender, EventArgs e)
{
if (textBoxDomainAddress.Text == "" || textBoxDomainAddress.Text == "" || textBoxPassword.Text == "" ||
textBoxSendMailAddress.Text == "" || textBoxSubject.Text == "" || textBoxContents.Text == "" || textBoxPort.Text == "")
{
MessageBox.Show("Lütfen tüm alanları doldurun.", "Hata Mesajı", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
SmtpServer.Host = textBoxDomainAddress.Text;
SmtpServer.Port = Convert.ToInt32(textBoxPort.Text);
SmtpServer.Credentials = new System.Net.NetworkCredential(textBoxUserName.Text, textBoxPassword.Text);
SmtpServer.EnableSsl = checkEnabled.Checked;
mail.From = new MailAddress(textBoxUserName.Text);
mail.To.Add(textBoxSendMailAddress.Text);
mail.Subject = textBoxSubject.Text;
mail.Body = textBoxContents.Text;
if (textBoxCC.Text != string.Empty)
{
mail.CC.Add(textBoxCC.Text);
}
if (textBoxAttach.Text !=string.Empty)
{
mail.Attachments.Add(new Attachment(attachFile));
}
SmtpServer.Send(mail);
MessageBox.Show("Mail gönderildi.", "Mail Gönderimi",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"HATA",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
private void btnAttach_Click(object sender, EventArgs e)
{
openFileDialogAttach.ShowDialog();
textBoxAttach.Text = openFileDialogAttach.SafeFileName;
attachFile = openFileDialogAttach.FileName;
}
}
}
