• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2146
  • Last Modified:

asp.net 3.5 System.Net.Mail

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

0
avaj
Asked:
avaj
  • 5
  • 3
1 Solution
 
aibusinesssolutionsCommented:
This line right here:
SmtpClient mySmtpClient = new SmtpClient();

You will want to specify the mail server there.
SmtpClient mySmtpClient = new SmtpClient("mail.whatever.com");
0
 
aibusinesssolutionsCommented:
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

0
 
avajAuthor 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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aibusinesssolutionsCommented:
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.
0
 
avajAuthor 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?
0
 
aibusinesssolutionsCommented:
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

0
 
Kumaraswamy RCommented:
Hi
I enclose sample Project , Try it, I hope it slove ur problem
0
 
aibusinesssolutionsCommented:
Did my last code snippet solve your problem?
0
 
avajAuthor Commented:
Perfect - thank you!

This is my first site using .net - so I'm sure I'll be back with more questions.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now