Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I have developed contact us for my client(one company for that i am developing website). in that i have write a code for sent a mail. I sent a mail successfully. I am using stmp.live.com for host and useraname is my email id (info@dhuvara.com)from my mail server and with password. Here destination email (To address) is my client email id(ex; info@domail.com). whoever visited their website sent enquiry from contact us page by sending details including their email id as a from address. but my problem is in destination emaild inbox they got mail from my address(info@dhuvara.com, this is shows as the from address).but i want the from address must be the address that typed from the contact us page from address.

Thanks in Advance
Posted

1 solution

when you create MailMessage object the property "From" of MailMessage object is the actual from address where client see where the mail comes.

C#
string mailFrom = "test@gmail.com";
var mailMessage = new MailMessage
            {
                Subject = "subject",
                Body = "body",
                IsBodyHtml = true,    
                From = new MailAddress(mailFrom)
            };

var smtpClient = new SmtpClient
            {
                Host = Configuration.Current.Smtp.Host,
                Port = Configuration.Current.Smtp.Port,                
            };
            if (!string.IsNullOrEmpty(Configuration.Current.Smtp.UserId))
            {
                smtpClient.Credentials = new NetworkCredential(Configuration.Current.Smtp.UserId, Configuration.Current.Smtp.Password);
            }
            mailClient.Send(message);    
 
Share this answer
 
Comments
baskaran chellasamy 30-Dec-13 3:14am    
ok this is my code. but from address is info@dhuvara.com
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("info@chapatimakingmachine.org");
Msg.Subject = txtEmail.Text;

// string message = PopulateBody(txtName.Text,txtphone.Text,txtEmail.Text,txtMessage.Text);
Msg.Body = "<html><body><Table><tr><td>Name :</td><td>" + txtName.Text + "</td></tr><tr><td>Phone Number :</td><td>" + txtphone.Text + "</td></tr><tr><td>Email Addresss :</td><td>" + txtEmail.Text + "</td></tr><tr><td>Booking Category :</td><td>" + Bookingcategordropdown.SelectedItem.Text + " </td></tr><tr><td> Message :</td><td>" + txtMessage.Text + "</td></tr></Table></body></html><html><body>";
Msg.CC.Add("sundhar@chapatimakingmachine.org");
Msg.CC.Add(txtEmail.Text);
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.live.com";
smtp.Port = 587;
Msg.Priority = MailPriority.Normal;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("info@dhuvara.com", "SolaiMail");
smtp.EnableSsl = true;
// smtp.Send(txtEmail, "info@dhuvara.com", txtEmail.Text, body);
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
// txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex.ToString());
}
S. M. Ahasan Habib 30-Dec-13 3:37am    
in your code i see from address is
Msg.From = new MailAddress(txtEmail.Text);
here you can change or update and set what email you can use
baskaran chellasamy 30-Dec-13 3:54am    
sorry. from address is dynamic email address that has typed by some of visitor of web page. so i cannot set any default address as a from address. In contact us page any visitor can come and will send their enquiry to the owner of the website.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900