Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello all,

I was working on project where i had to send emails to the users in my application, i used asp.net code as below

public static bool SendEmail(string sender, string password, string receiver, string message, string subject)
{
try
{
MailMessage mailMessage = new MailMessage(sender, receiver);
mailMessage.Subject = subject;
//Adding the Mail Body
mailMessage.Body = message;
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 25;
smtpClient.Credentials = new System.Net.NetworkCredential(sender, password);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
return true;
}
catch
{
return false;
}
}

Here i need to pass username and password of sender as parameters,
what if i don't have password of user, as my users won't give me their passwords.
I want my application to send email from any sender to any receiver without asking for password.
i have seen some php snippets which does that... but if php can do... then my dotnet should also do it for me thats what i believe.
If anybody know's about sending email without senders password please let me know.

Thanks in Advance.
SUNIL MALI.
Posted
Comments
bbirajdar 21-Mar-13 6:45am    
Dear Sunil

To send a email from somebody else's account is illegal. You are liable for legal actions if you do so. Hacking and spam related questions are not encouraged and answered on CP. So you will not get the answer for this question here

Hi!!! Sunil!! Yes you have to do this from ip address below i m giving you a code use it. in this password is not required,,,,


C#
protected void btnsubmit_Click1(object sender, EventArgs e)
   {
       try
       {

              System.Web.Mail.MailMessage Msg = new System.Web.Mail.MailMessage();
              // Sender e-mail address.
              Msg.From = txtemail.Text;
             // Recipient e-mail address.
              Msg.To = "info@user.com";
              Msg.Subject = "Enquiry";
              Msg.Body = "Name : " + txtname.Text + "\n" + "Mobile : " + txtsubject.Text + "\n" + "Query : " + txtmsg.Text;
              // your remote SMTP server IP.
             SmtpMail.SmtpServer = "67.225.221.112";//your ip address
             SmtpMail.Send(Msg);
             Msg = null;
            Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='contactus.aspx';}</script>");

            txtemail.Text = "";
            txtmsg.Text = "";
            txtname.Text = "";
            txtsubject.Text = "";
          }
           catch (Exception ex)
            {
                Page.RegisterStartupScript("UserMsg", "<script>alert('Mail not sent ');if(alert){ window.location='page.aspx';}</script>");
            }

   }
 
Share this answer
 
v2
Comments
bbirajdar 21-Mar-13 6:42am    
Wrong solutions 1 & 2 ... What the OP wants is clearly mentioned in his question -

"I want my application to send email from any sender to any receiver without asking for password."

He wants to send email from the user's email id without using the user's password and NOT the SMTP password.... In simplest terms , if you are a user of his website then he wants to send email from yourname@gmail.com or yourname@hotmail.com WITHOUT using YourPassword@123
Shubham Choudhary 21-Mar-13 6:52am    
ok!!! thaks for comment!!!! @aspnet_regiis -i
Shubham Choudhary 21-Mar-13 6:57am    
and regiis sir!!! but in this code i am using ip address and you saying in above which scenario i am also faceing this.. if he made change on it and use he will geting his result corrextly
sunil mali 22-Mar-13 2:21am    
Thank you all,
Shubham sir and aspnet_regiis sir,
With the help of my network administrator i configured smtp on one of our machine.
He gave me one ip address and i used it for sending email....
its sending emails... no limitations(as if u send it via gmails server u can send only 500 emails in a day)... and from gmail account only... . its sending through my servers iis smtp.... i can write any email as sender and send it to anybody i want... and password too....
so i achieved my task of sending emails to them.... obviously your help triggered me of running smtp on one of our server... i got static ip from them... and i sent email through code...
Thank you all....

Regards,
SUNIL MALI.
Member 11718522 7-Dec-16 23:44pm    
I am also facing this problem can you post the code of this solution
Hi,

Your code snippet is working with gmail smtp.
Assuming that you are working with an exchange server, you can change your code to:

Before:
C#
smtpClient.Credentials = new System.Net.NetworkCredential(sender, password);


After:
C#
smtpClient.Credentials = System.Net.CredentialCache.DefaultCredentials;


with gmail smtp this solution won't work.

probably you will need to impersonate the user account before make the call, otherwise the credentials will be from the asp.net process.
 
Share this answer
 
v2
Thank you all,
Shubham sir and aspnet_regiis sir,
With the help of my network administrator i configured smtp on one of our machine.
He gave me one ip address and i used it for sending email....
its sending emails... no limitations(as if u send it via gmails server u can send only 500 emails in a day)... and from gmail account only... . its sending through my servers iis smtp.... i can write any email as sender and send it to anybody i want... and password too....
so i achieved my task of sending emails to them.... obviously your help triggered me of running smtp on one of our server... i got static ip from them... and i sent email through code...
Thank you all....

Regards,
SUNIL MALI.
 
Share this answer
 
v2
Comments
Libino Giffrin 23-Feb-17 5:59am    
I am also facing this problem can you post the code of this solution

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