views:

222

answers:

2

I am unable to send the mail using smtp client. here is the code:

SmtpClient client=new SmtpClient("Host");
client.Credentials=new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.from="[email protected]";
mailMessage.To.Add("[email protected]");
mailMessage.body="body";
mailMessage.subject="subject";
client.Send(mailMessage);

The problem is that when I use this code in ASP.NET application, I do not recieve any mails. When in asp.net I change the from mail address to username given in NetworkCredential, I recieve mails.

But in C# windows application, I can get emails, even if sender's email address is not valid.

Thanks in advance....

+2  A: 

It means your mail server does not allow Mail-Relay. Your mail server only allows you to send mail from authenticated email-id as username. Generaly this is done to prevent mails being sent as different identities other than the authenticated one.

this. __curious_geek
The mail server is same in both windows as well as ASP.NET application.And also the same code is working fine in other page of ASP.NET application. Its not working in the login page of ASP.NET application.What can be the problem?
Manish Gupta
What is Authentication Mode of your application ? Please provide more details around your problem scenario..
this. __curious_geek
In asp.net authentication mode is set to forms.
Manish Gupta
A: 

try this :

{ MailMessage mail = new MailMessage("emailfrom","emailto");

        mail.From = new MailAddress("emailfrom");
        mail.Subject = txtsbjct.Text;
        string Body = txtmsg.Text;
        mail.Body = Body;

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
      ("youremail", "yourpassword");



        smtp.EnableSsl = true;
        smtp.Send(mail);
        txtemail.Text = "";
        txtmsg.Text = "";
        txtsbjct.Text = "";
        Label1.Text = "your email has been send";
        mail = null;
        smtp = null;
    }

by colonel

colonel