Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i am using contact us page to send feedback.In that i am getting the from address in emailid textbox and passins as parameter.Mail is working perfectly,but the from address is not getting by the parameter.

can anyone help me to solve this?

Here is my code

C#
MailMessage mail = new MailMessage();
        mail.IsBodyHtml = true;
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.Priority = MailPriority.High;
        string fromaddrs = @"fromadd@gmail.com";
        string to = @"toadd@gmail.com";
        mail.From = new MailAddress(fromaddrs);
         mail.To.Add(to);
        mail.Subject =subject;
        string body = "<table width="527" border="0" cellspacing="1" cellpadding="1">" +
                        "<tr>" +
                            "<td width="20%" height="20">Name: </td>" +
                            "<td width="80%">" + name + "</td>" +
                        "</tr>" +
                        "<tr>" +
                            "<td height="20">EmailId:</td>" +
                            "<td>" + emailid + "</td>" +
                        "</tr>" +
                        "<tr>" +
                            "<td height="20">MobileNo.:</td>" +
                            "<td>" + mobile + "</td>" +
                        "</tr>" +
                          "<tr>" +
                            "<td height="20">Address:</td>" +
                            "<td>" + address + "</td>" +
                        "</tr>" +
"<tr>" +
                            "<td height="20">Message:</td>" +
                            "<td>" + msg + "</td>" +
                        "</tr>" +
                        "</table>";
        mail.Body = body.ToString();
        SmtpClient smtpclt = new SmtpClient();
        smtpclt.Credentials = new System.Net.NetworkCredential(to, "*****");
        smtpclt.Port = 587;
        smtpclt.Host = "smtp.gmail.com";
        smtpclt.EnableSsl = true;
        smtpclt.DeliveryMethod = SmtpDeliveryMethod.Network;
        try
        {
            smtpclt.Send(mail);
            return 1;
        }
        catch (Exception error)
        {
            return -1;
        }
Posted
Updated 23-Feb-12 23:38pm
v2

Use
C#
smtpclt.Credentials = new System.Net.NetworkCredential(fromaddrs, "*****");
 
Share this answer
 
Comments
AmitGajjar 24-Feb-12 7:33am    
+5 correct. credentials you are passing would be TO address in your email.
Try the three parameter overload:
C#
mail.From = new MailAddress(fromaddrs, "Feedback @ mySite.com", Encoding.UTF8);
If that doesn't help, we need more info on what doesn't happen / does happen that shouldn't.
 
Share this answer
 
Comments
Thendral.N 24-Feb-12 5:47am    
if i gave from address fromadd@gmail.com in email id textbox and to address as toadd@gmail.com.but it will take both as same like toadd@gmail.com
Thendral.N 24-Feb-12 5:51am    
to address is passing correctly but from address only not getting..it will take to address as from address
OriginalGriff 24-Feb-12 6:08am    
It's probably a language problem but I don't understand what you are saying. Do you have a short example?
Thendral.N 24-Feb-12 6:18am    
Name: thens
EmailId: thens@gmail.com
MobileNo.: 986598659
Address: Chennai
Message: Course

these are the details i am getting from contact us page.
my to mail id is:toadd@gmail.com
my from mail id is:fromadd@gmail.com

but it will take to address as from address.
In my inbox it will show from address as also toadd@gmail.com and to address as toadd@gmail.com
OriginalGriff 24-Feb-12 7:25am    
Your code looks right - the only difference between yours and what I use (http://www.codeproject.com/Tips/163829/Sending-an-Email-in-C-with-or-without-attachments) is the order in which things are loaded. Oh, and I don't use gmail as I have my own domain.
Out of interest, try it again, and send it to a different destination email address (WillExpire25Feb2012@Software-Matters.com) and let's see what happens...

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