Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am developing a web application which has a concept of sending email.Once the user gives the receiver's mail id and content,the mail will be sent from the user's mail id(Which would be unique for all the user's).This is my code


C#
if (Page.IsValid)
{
   MailMessage mailmessage = new MailMessage();
   mailmessage.From = new MailAddress(test.Text);
   mailmessage.To.Add(new System.Net.Mail.MailAddress(Textemail.Text));
   mailmessage.Subject = TextSub.Text;
   mailmessage.Body = "<b>Query from : </b>" + Textname.Text + "<br/>" +
                     "<b>Sender Details : </b>" + Textemail.Text+ "<br/>" +
                     "<b>Queries Asked : </b>" + Textcomment.Text + "<br/>";
                
   mailmessage.IsBodyHtml = true;
                 
   SmtpClient smtpclient = new SmtpClient("smtp.gmail.com", 587);
   smtpclient.EnableSsl = true;

   smtpclient.Credentials = new System.Net.NetworkCredential("***@gmail.com","");
   smtpclient.Send(mailmessage);
   SuccessLabel.Text = "Your Query has been successfully sent to Appropriate mail id";
                 
   Textname.Enabled = false;
   Textemail.Enabled = false;
   TextSub.Enabled = false;
   Textcomment.Enabled = false;
   SendMsg.Enabled = false;
}

Here in credentials I cant give the username and password of the sender's because it depends on the user who logs in.So how to use the credentials for each separate user who logs in?Please help.!!Thanks in advance!!
Posted
Updated 3-Nov-14 11:55am
v3
Comments
Kornfeld Eliyahu Peter 3-Nov-14 12:53pm    
The credentials you provide here used to identify a user account on the SMTP server, so the first question is: do you have an SMTP account for every user who logs-in?
DamithSL 3-Nov-14 12:57pm    
are you using gmail or some other SMTP server?

1 solution

The only way to provide the user's GMail credentials would be to ask the user to enter them. Any user with an ounce of common sense wouldn't enter their GMail credentials on a random website, so I don't think that's going to work. :)

You need to provide the credentials of your own GMail account. In order to send the message, you'll need to set the Sender property[^] to your own email address.
C#
mailmessage.From = new MailAddress(test.Text);
mailmessage.Sender = new MailAddress("Your-Address@gmail.com");
...
smtpclient.Credentials = new System.Net.NetworkCredential("Your-Address@gmail.com", "Your-Password");

Depending on the recipient's mail client, the message could be shown as "From <your address> on behalf of <user's address>".
 
Share this answer
 

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