.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Sending mail to Recipient with name of Secondary Mail address by using Primary Mail address

Posted By:Pravin       Posted Date: September 08, 2011    Points: 40    Category: Email    URL: http://www.dotnetspark.com  

To use this Secondary mail-id as from address , the Mx-record should be fix Secondary mail to Primary mail-id with the appropriate server.
 

using System.Net.Mail;

 //namespace is required to sendmail


protected void Button1_Click(object sender, EventArgs e)

        {


         string sender="ABC@Company.com";
   //Replace this with your own correct Company Primary Address 

       string from = "xyz@Company.com";
            //Replace this with your own correct Company Secondary Address

                    
            string to = "zyx@anymail.com";

            //Replace this with the Email Address to whom you want to send the mail

            string replyto = "ABC@Company.com";

            //Replace this with the Email Address to whom recepient have to reply

            MailMessage mail = new MailMessage();

            mail.To.Add(to);

            mail.From = new MailAddress(from, "AliasName For Address" , System.Text.Encoding.UTF8);

   mail.Replyto =  new MailAddress(replyto);

            mail.Subject = "This is a test mail" ;

            mail.SubjectEncoding = System.Text.Encoding.UTF8;

            mail.Body = "This is Email Body Text";

            mail.BodyEncoding = System.Text.Encoding.UTF8;

            mail.IsBodyHtml = true ;

            mail.Priority = MailPriority.High;

            Attachment att1 = new Attachment(@"D:\ABC\Sample.txt");

            mail.Attachments.Add(att1);

            SmtpClient client = new SmtpClient();

            //Add the Creddentials- use your Primary email id and password


            client.Credentials = new System.Net.NetworkCredential(sender, "xxxxxxxxxx");

 
            client.Port = XXX; // Port number

            client.Host = "smtp.XXXXX.com";//Place the smtp server name

            client.EnableSsl = true; 

            try

            {

            client.Send(mail);

            }

            catch (Exception ex)

            {

            Exception ex2 = ex;

            string errorMessage = string.Empty;

            while (ex2 != null)

            {

                errorMessage += ex2.ToString();

                ex2 = ex2.InnerException;

            }

            HttpContext.Current.Response.Write(errorMessage );

             } // end try

        }
using System.Net.Mail;

 //namespace is required to sendmail


protected void Button1_Click(object sender, EventArgs e)

        {


      	    string sender="ABC@Company.com";
	    //Replace this with your own correct Company Primary Address 

    	    string from = "xyz@Company.com";
            //Replace this with your own correct Company Secondary Address

                    
            string to = "zyx@anymail.com";

            //Replace this with the Email Address to whom you want to send the mail

            string replyto = "ABC@Company.com";

            //Replace this with the Email Address to whom recepient have to reply

            MailMessage mail = new MailMessage();

            mail.To.Add(to);

            mail.From = new MailAddress(from, "AliasName For Address" , System.Text.Encoding.UTF8);

	    mail.Replyto =  new MailAddress(replyto);

            mail.Subject = "This is a test mail" ;

            mail.SubjectEncoding = System.Text.Encoding.UTF8;

            mail.Body = "This is Email Body Text";

            mail.BodyEncoding = System.Text.Encoding.UTF8;

            mail.IsBodyHtml = true ;

            mail.Priority = MailPriority.High;

            Attachment att1 = new Attachment(@"D:\ABC\Sample.txt");

            mail.Attachments.Add(att1);

            SmtpClient client = new SmtpClient();

            //Add the Creddentials- use your Primary email id and password


            client.Credentials = new System.Net.NetworkCredential(sender, "xxxxxxxxxx");

 
            client.Port = XXX; // Port number

            client.Host = "smtp.XXXXX.com";//Place the smtp server name

            client.EnableSsl = true; 

            try

            {

            client.Send(mail);

            }

            catch (Exception ex)

            {

            Exception ex2 = ex;

            string errorMessage = string.Empty;

            while (ex2 != null)

            {

                errorMessage += ex2.ToString();

                ex2 = ex2.InnerException;

            }

            HttpContext.Current.Response.Write(errorMessage );

             } // end try

        }



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend