ASP.NET ile SMTP mail göndermek için aşağıda yazdığım Class’tan faydalanabilirsiniz. ASP.NET’te mail ayarları için web.config
‘te ayarlama yapmanız gerekiyor.
ASP.NET için Mail.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
namespace DavasWeb.Models
{
public class Mail
{
public static bool SendMail(MailData MailData)
{
try
{
if (string.IsNullOrEmpty(MailData.ReceiverMails))
{
return false;
}
MailSettings MailSettings = GetMailSettings();
MailMessage Mail = new MailMessage()
{
From = MailSettings.MailSender,
Subject = MailData.Subject,
SubjectEncoding = Encoding.UTF8,
Body = MailData.MailContent,
BodyEncoding = Encoding.UTF8,
IsBodyHtml = true
};
List<string> ReceiverList = MailData.ReceiverMails.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string ReceiverMail in ReceiverList)
Mail.To.Add(ReceiverMail);
if (!string.IsNullOrEmpty(MailData.CcMails))
{
List<string> CcList = MailData.CcMails.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string CcMail in CcList)
Mail.CC.Add(CcMail);
}
if (!string.IsNullOrEmpty(MailData.ReplyMails))
{
List<string> ReplyList = MailData.ReplyMails.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
Mail.ReplyToList.Add(string.Join(",", ReplyList));
}
SmtpClient MailClient = new SmtpClient()
{
Host = MailSettings.MailServer,
Port = MailSettings.MailPort,
EnableSsl = MailSettings.MailEnableSsl,
UseDefaultCredentials = false,
Credentials = MailSettings.MailUser,
DeliveryMethod = SmtpDeliveryMethod.Network
};
MailClient.Send(Mail);
return true;
}
catch (Exception e)
{
return false;
}
}
private static MailSettings GetMailSettings()
{
return new MailSettings()
{
MailSender = new MailAddress(ConfigurationManager.AppSettings["mailSenderMail"], ConfigurationManager.AppSettings["mailSender"], Encoding.UTF8),
MailServer = ConfigurationManager.AppSettings["mailServer"],
MailPort = Int32.Parse(ConfigurationManager.AppSettings["mailPort"]),
MailEnableSsl = ConfigurationManager.AppSettings["mailEnableSsl"] == "true",
MailUser = new NetworkCredential(ConfigurationManager.AppSettings["mailUsername"], ConfigurationManager.AppSettings["mailPassword"])
};
}
}
public class MailData
{
public string Subject { get; set; }
public string MailContent { get; set; }
public string ReceiverMails { get; set; } //Mailler noktalı virgül ile ayrılmalı.
public string CcMails { get; set; } //Mailler noktalı virgül ile ayrılmalı.
public string ReplyMails { get; set; } //Mailler noktalı virgül ile ayrılmalı.
}
partial class MailSettings
{
public MailAddress MailSender { get; set; }
public string MailServer { get; set; }
public int MailPort { get; set; }
public bool MailEnableSsl { get; set; }
public NetworkCredential MailUser { get; set; }
}
}
ASP.NET için web.config
<!-- Sizde appSettings varsa sadece içini almanız yeterli. -->
<appSettings>
<!-- Mail Settings -->
<add key="mailServer" value="sunucu" />
<add key="mailPort" value="587" />
<add key="mailEnableSsl" value="false" />
<add key="mailUsername" value="kullanici_adi" />
<add key="mailPassword" value="sifre" />
<add key="mailSender" value="gonderen" />
<add key="mailSenderMail" value="gonderen_mail" />
</appSettings>
Bu da kodun sade hali konsol ve form uygulamalarınız için kullanabilirsiniz.
Gerekli kütüphaneler;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
MailMessage Mail = new MailMessage()
{
From = new MailAddress("gonderen_mail", "gonderen_ad_soyad", Encoding.UTF8),
Subject = "konu",
SubjectEncoding = Encoding.UTF8,
Body = "mesaj", //HTML olabilir
BodyEncoding = Encoding.UTF8,
IsBodyHtml = true
};
Mail.To.Add("alici_mail_1");
Mail.To.Add("alici_mail_2");
Mail.To.Add("alici_mail_3");
Mail.CC.Add("cc_mail_1");
Mail.CC.Add("cc_mail_2");
Mail.CC.Add("cc_mail_3");
Mail.ReplyToList.Add("cevap_mail_1,cevap_mail_2,cevap_mail_3");
SmtpClient MailClient = new SmtpClient()
{
Host = "sunucu", //string
Port = port_no, //int
EnableSsl = ssl_var_mi, //bool
UseDefaultCredentials = false,
Credentials = new NetworkCredential("kullanici_adi", "sifre"),
DeliveryMethod = SmtpDeliveryMethod.Network
};
MailClient.Send(Mail);