• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2155
  • 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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