Pages

Wednesday, August 21, 2013

How to Send Mail from Office Email Id in Asp.net

In previous article we discussed How to Send Mail in asp.net where we used Gmail domain for send mail. Buts in this article we are going to explain how to send mail using office email id or office domain. To send mail firstly we have to use a namespace 'System.Net.Mail'. After add namespace write following code on Button click in 'Default.aspx.cs' page.

Send mail using office domain

protected void SendBtn_Click(object sender, EventArgs e)
    {
        MailMessage mailMsg = new MailMessage();

        mailMsg.To.Add(new MailAddress("receiver@gmail.com")); //Receiver Email Id
        mailMsg.From = new MailAddress("sender@companyname.com"); //Your office email id
        mailMsg.Subject = "mail test";
        mailMsg.Body = "Hello Friends";
        mailMsg.BodyEncoding = System.Text.Encoding.UTF8;
        mailMsg.SubjectEncoding = System.Text.Encoding.UTF8;

        SmtpClient smtp = new SmtpClient();
        smtp.Port = 25;
        smtp.Host = "mail.companyname.com"; //Host name of office domain
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential("sender@companyname.com", "password");
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = nc;
        smtp.Send(mailMsg);

        try
        {
            smtp.Send(mailMsg);
            Response.Write("Your Mail has been Sent");
        }

        catch (Exception er)
        {
            Response.Write("Error in Mail Sending");
        }

    }

No comments:

Post a Comment