We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

asp.net 3.5 System.Net.Mail

Medium Priority
2,172 Views
Last Modified: 2012-05-06
new to .net and trying to create a contact form.  I have tried the example that I have in a book which works on my local machine but not on the production server.  I think I'm just leaving out the domain name i.e., mail.name.com but I'm not sure what the syntax is (or if that is the problem). Below is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
 
public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txtHomePhone.Text != string.Empty || txtBusPhone.Text != string.Empty)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
            string mailBody = System.IO.File.ReadAllText(fileName);
 
            mailBody = mailBody.Replace("##Name##", txtName.Text);
            mailBody = mailBody.Replace("##Email##", txtEmail.Text);
            mailBody = mailBody.Replace("##HomePhone##", txtHomePhone.Text);
            mailBody = mailBody.Replace("##BusPhone##", txtBusPhone.Text);
            mailBody = mailBody.Replace("##Coments##", txtComments.Text);
            MailMessage myMessage = new MailMessage();
            myMessage.Subject = "Response From Metsynd Website";
            myMessage.Body = mailBody;
 
            myMessage.From = new MailAddress  "postmaster@somewhere.com", "SomeWebSite" ");
            myMessage.To.Add(new MailAddress("me@hotmail.com", "Admin"));
 
            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Send(myMessage);
 
 
            lblMessage.Visible = true;
            FormTable.Visible = false;
 
 
        }
            
 
}
 
 
 
 
        
    }

Open in new window

Comment
Watch Question

This line right here:
SmtpClient mySmtpClient = new SmtpClient();

You will want to specify the mail server there.
SmtpClient mySmtpClient = new SmtpClient("mail.whatever.com");
Also, if you need to use SMTP Authentication to send mail, you can do this:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential myCredential = 
    new NetworkCredential("username", "password"); 
smtpClient.Host = "mail.whatever.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = myCredential;

Open in new window

Author

Commented:
SmtpClient mySmtpClient = new SmtpClient("mail.whatever.com"); - I tried  that earlier I get a message that says the name "mail" does not exist in the current context.

Not sure if I need authentication or not; but I'll try it
When you tried it earlier, did you have mail.server.com enclosed in quotes?  "mail.server.com"

If not it would look for a variable named mail, which doesn't exist in the current context.

Author

Commented:
I do need authentication but when I inserted your code below - I get "NetworkCredential could not be found - are you missing a using directive or assembly reference"

SmtpClient smtpClient = new SmtpClient();
NetworkCredential myCredential =
    new NetworkCredential("username", "password");
smtpClient.Host = "mail.whatever.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = myCredential;

I also added:    myCredential.send(myMessage);

would that be correct?
No, here is your code with smtp authentication.
myMessage.From = new MailAddress  "postmaster@somewhere.com", "SomeWebSite" ");
myMessage.To.Add(new MailAddress("me@hotmail.com", "Admin"));
 
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = 
    new System.Net.NetworkCredential("username", "password"); 
mySmtpClient.Host = "mail.whatever.com";
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
 
mySmtpClient.Send(myMessage);

Open in new window

CERTIFIED EXPERT

Commented:
Hi
I enclose sample Project , Try it, I hope it slove ur problem
Did my last code snippet solve your problem?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Perfect - thank you!

This is my first site using .net - so I'm sure I'll be back with more questions.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.