Send email failed in asp.net

Hello, I have a short code to test sending email in my asp.net application. But when I check the email, nothing there.
Why?
 string from = ((TextBox)CreateUserWizardStep2.ContentTemplateContainer.FindControl("Email")).Text;
            string to = "somebody@yahoo.com";
            string UserName = User.UserName;
            string Email = User.Email;
            string body = string.Format("User Name: {0} Email: {1}   ", UserName,Email);
            MailMessage mailObj = new MailMessage(from, to, "Request Account", body);
            Response.Redirect("https://www.myweb.com/");
            Session.Abandon();

Open in new window

And to create a good view email body, I mean lines are wrapped.
Thanks.
zhshqzycAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TonyRebaCommented:
Use .Net.Mail

sample code
0
TonyRebaCommented:
Code
using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
            txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
        SmtpClient SMTPServer = new SmtpClient("localhost");
        try
        {
           SMTPServer.Send(mailObj);
        }
            catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }
}

Open in new window

0
zhshqzycAuthor Commented:
How to identify the replacement of "localhost"?
Suppose I want to send the email to abc@myweb.com.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

TonyRebaCommented:
localhost is the server you are sending the email from, you can use localhost, name, or ip address,

In the txtFrom you add the sender account, and to I suppose will be read from whatever the user inputs their email


See another example below
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
   MailAddress fromAddress = new MailAddress("from@site.com","Nameofsendingperson");
   message.From = fromAddress;//here you can set address
   message.To.Add("to@site.com");//here you can add multiple to
   message.Subject = "Feedback";//subject of email
   message.CC.Add("cc@site.com");//ccing the same email to other email address
   message.Bcc.Add(new MailAddress("bcc@site.com"));//here you can add bcc address
   message.IsBodyHtml = false;//To determine email body is html or not
   message.Body =@"Plain or HTML Text";
   smtpClient.Send(message);
}
catch (Exception ex)
{
   //throw exception here you can write code to handle exception here
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zhshqzycAuthor Commented:
My code:
StringBuilder sb = new StringBuilder();
            sb.Append("User Information:").Append(Environment.NewLine);
            sb.Append(string.Format("First Name: {0}", FirstName)).Append(Environment.NewLine);
            sb.Append(string.Format("Last Name: {0}", LastName)).Append(Environment.NewLine);
            sb.Append(string.Format("Degree: {0}", Degree)).Append(Environment.NewLine);
            sb.Append(string.Format("Phone: {0}", Phone)).Append(Environment.NewLine);
            sb.Append(string.Format("Organization: {0}", Org)).Append(Environment.NewLine);
            string body = sb.ToString();
            MailMessage mailObj = new MailMessage(from, to, "Request Account", body);
            SmtpClient smtpClient  = new SmtpClient();
            smtpClient.Send(mailObj);

Open in new window

Then I got an error:
System.InvalidOperationException: The SMTP host was not specified.

Open in new window

0
TonyRebaCommented:
Replace
SmtpClient smtpClient  = new SmtpClient();
with
SmtpClient smtpClient  = new SmtpClient("localhost");  //Or whatever your stmp client is
0
zhshqzycAuthor Commented:
Yes, but how to find it if it is not "localhost". Suppose somebody who is outside of our organization wants to send email to us.
If his email is given such as "abc@1234.com".
0
TonyRebaCommented:
You are only sending email from this web form, that is way you need a smtp client in your webserver , you still can receieve emails from any outside outbox .
0
zhshqzycAuthor Commented:
No, you may misunderstood my question or I could misunderstood your opinions. Somebody outside is going to apply an account from this web form, after he completes it. He will send email to inform us. The message includes his name and phone etc. Please look at the code above.
But there are so many organizations, do we set up an unique smtp information.
0
TonyRebaCommented:
Where is the website hosted at ? If the answer is in one server you have access to , then you need to have a smtp usally in port 25 and define it in your form code .
0
zhshqzycAuthor Commented:
The website will be hosted by ourself, so do you you mean we only to put our smtp in the code?
0
zhshqzycAuthor Commented:
I put the smtp into it. But got an error:
System.Net.Mail.SmtpFailedRecipientException was unhandled by user code
  Message=Mailbox unavailable. The server response was: 5.7.1 Unable to relay

Open in new window



Then the code is:
StringBuilder sb = new StringBuilder();
            sb.Append("User Information:").Append(Environment.NewLine);
            sb.Append(string.Format("First Name: {0}", FirstName)).Append(Environment.NewLine);
            sb.Append(string.Format("Last Name: {0}", LastName)).Append(Environment.NewLine);
            sb.Append(string.Format("Degree: {0}", Degree)).Append(Environment.NewLine);
            sb.Append(string.Format("Phone: {0}", Phone)).Append(Environment.NewLine);
            sb.Append(string.Format("Organization: {0}", Org)).Append(Environment.NewLine);
            string body = sb.ToString();
            MailMessage mailObj = new MailMessage(from, to, "Request Account", body);
            SmtpClient smtpClient  = new SmtpClient("some.com");
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailObj);

Open in new window

0
TonyRebaCommented:
Try using localhost as I suggested :

 SmtpClient smtp = new SmtpClient();
 smtp.Host = "localhost"; //Or Your SMTP Server Address
0
zhshqzycAuthor Commented:
No error in the debug now but no email in the inbox as well.
0
TonyRebaCommented:
To things specify a port (25) make sure is open

and debug by adding try catch block
try{
    sendMail();
  }
  catch(Exception ex)
  {

  }
}

Open in new window

0
zhshqzycAuthor Commented:
I got the email, it took for a while.
0
TonyRebaCommented:
Glad it works!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.