Mangesh barmate

Mangesh barmate

  • NA
  • 118
  • 132.7k

Get SMTP detail from domain name in c#?

Nov 9 2015 6:17 AM

Hi Team,

Below are my working code-

MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
string fromEmail = "[email protected]";
mail.From = new MailAddress(fromEmail);
string[] split = fromEmail.Split('@');
string domainName = split[1].ToString();

mail.Subject = "Email using any SMTP";

string Body = "Hi, this mail is to test sending mail" + "using any SMTP in ASP.NET";
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.mail.yahoo.com";
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "*********");
smtp.EnableSsl = true;
smtp.Send(mail);

This code is perfectly working for Yahoo SMTP server. Now what is my requirement here,

suppose, i have hotmail,gmail or any other email address, i will get it Domain Name into domainName variable. I want to get SMTP mail server name on that Domain Name and pass it to smtp.Host value. In this way i want to keep my code dynamic.

Can anyone suggest me the approach for how to get SMTP mail server name on the base of Domain Name?

Please suggest.


Answers (1)