Sunday, 15 May 2011

Send email using C# ASP.NET

In Web.config

  <!--
  <system.net>
    <mailSettings>
      <smtp from="info@bdculture.com">
        <network
             host="mail.bdculture.com"
             port="25"
             userName="info@bdculture.com"
             password="info123" 
             defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>
  -->
===========================
<system.net>
        <mailSettings>
            <smtp deliveryMethod="Network" from="myid@yahoo.com">
                <network host="smtp.mail.yahoo.com" port="465" userName="myid@yahoo.com" password="mypassword" defaultCredentials="true" />
            </smtp>
        </mailSettings>
    </system.net>

***************************
SmtpClient smtp = new SmtpClient();
        smtp.Host = SimpleTopupConfiguration.MailServer;
        smtp.Credentials = new System.Net.NetworkCredential("info@bdculture.com", "info123");
        smtp.Port = 25;


        MailMessage mail = new MailMessage();
        string from = SimpleTopupConfiguration.FromMail;
        mail.From = new MailAddress(from);
        string sendTo = SimpleTopupConfiguration.SendTo;
        mail.To.Add(sendTo);
        string subject = subjectTextBox.Text.ToString();
        mail.Subject = subject;
        string body = 
            "Name: " + nameTextBox.Text.ToString()
            + "\n" + "Organization: " +  organizationTextBox.Text.ToString()
            + "\n" + "Telephone: " + telephoneTextBox.Text.ToString()
            + "\n" + "Email from: " + emailTextBox.Text.ToString()
            + "\n" + "Message: "
            + "\n" + commentsTextBox.Text.ToString();
        mail.Body = body;


        smtp.Send(mail);
*************************

Web.config:

<appSettings>
    <add key="MailServer" value="mail.pcl.ac"/>
    <add key="Port" value="25"/>
    <add key="FromMail" value="masud.ahmed@pcl.ac"/>
    <add key="Keyword" value="!1pcl000"/>
    <add key="SendToMail" value="masuds_mail@yahoo.com" />
  </appSettings>


Mail function .aspx.cs file:
protected void sendEmailButton_Click(object sender, EventArgs e)
{
        SmtpClient smtp = new SmtpClient();
        smtp.Host = MasudAhmedConfiguration.MailServer;
        smtp.Port = Int32.Parse(MasudAhmedConfiguration.Port);
        smtp.Credentials = new System.Net.NetworkCredential(MasudAhmedConfiguration.FromMail, MasudAhmedConfiguration.Keyword);
        MailMessage mailMsg = new MailMessage();
        mailMsg.To.Add(MasudAhmedConfiguration.SendToMail);
        mailMsg.From = new MailAddress(MasudAhmedConfiguration.FromMail);
        mailMsg.Subject = "Test Email";
        string body = bodyTextBox.Text.ToString();
        mailMsg.Body = body;
        //string senderEmail = yourEmailTextBox.Text.ToString();
        //mailMsg.From = new MailAddress(senderEmail);
        try
        {
            smtp.Send(mailMsg);
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }



===================

private void SendEmail(string from, string to, string subject, string body)
    {
      MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));

      mail.Subject = subject;
      mail.Body = body; 

      SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
      smtpMail.Port = 587;
      smtpMail.EnableSsl = true;
      smtpMail.Credentials = new NetworkCredential("xxx@gmail.com", "xxx");
      // and then send the mail
      smtpMail.Send(mail);
    }

Email address validation check:

Step 1: Simple validation
Simple validation basically checks that there's one "@" and one ".".

public static bool isEmail(string inputEmail)
{
  if( inputEmail == null || inputEmail.Lengt == 0 )
    {
        throw new ArgumentNullException( "inputEmail" );
    }

   string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
         @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
         @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
   Regex re = new Regex(strRegex);
   if (re.IsMatch(inputEmail))
    return (true);
   else
    return (false);
}

Step 2: DNS validation
The first real step is to make sure that the domain actually exists.

Step 3: SMTP validation
This method uses the SMTP protocol to contact the domain mailserver and actually ask if the user is valid!

No comments:

Post a Comment