5

I have a problem creating application to send email.

I already have one working as a Windows Forms Application and then decided to do the same from the empty project, because I need to make a background application now.

I used the System.Net.Mail namespace for that in a Windows Forms. Then added the same code to the new project.

However, I get the following error:

The type or namespace name 'Mail' does not exist in the namespace 'System.Net'.

The 'System.Net' reference is included in the project references list and I can't find anything named 'System.Net.Mail' in the list of possible references. The point is that I didn't have those errors in Windows Forms app.

NotMe
  • 87,343
  • 27
  • 171
  • 245
gthacoder
  • 2,213
  • 3
  • 15
  • 17
  • What version of .NET and Visual Studio are you using? `System.Net.Mail` was introduced in 2005/2.0, but in .NET 1.1/2003 it was called `System.Web.Mail`. – Dai Mar 22 '13 at 01:50
  • 2
    The `System.Net.Mail` classes are contained in the "System.dll" file. Make sure you have a reference to `System` listed under References in your project. – Jason Watkins Mar 22 '13 at 01:51
  • Yeah. All you say I know. I use the latest Visual Studio. Express Edition for Desktop. And I definitely add the reference of System.Net.Mail to the project references list. I will post the simplest code below. Try to create empty project and paste it. – gthacoder Mar 22 '13 at 02:01
  • It's not working out to past code in nice form, so just try to add "using System.Net; using System.Net.Mail;" to the empty project and add System.Net to references list. – gthacoder Mar 22 '13 at 02:09
  • Use a project template to keep out of trouble until you learn more about .NET – Hans Passant Mar 22 '13 at 03:09

2 Answers2

9

I tried to reproduce this in Visual Studio 2012, but even when I set the compiler to use .NET 2.0 I still did not encounter this issue.

It is possible that you are missing the reference for System.dll which includes the namespace System.Net.Mail. Just as an precaution as you didn't include any sample code I will include a simple implementation of a Mail client as an example.

using (SmtpClient client = new SmtpClient("smtp-server.MyDomain.com"))
{
    client.UseDefaultCredentials = true;

    using (MailMessage mail = new MailMessage())
    {
        mail.Subject = subject;
        mail.Body = body;

        mail.From = new MailAddress("MyEmail@MyDomain.com");
        mail.To.Add("ToThisEmail@MyDomain.com");

        client.Send(mail);
    }
}

Even though I wasn't able to reproduce this I would still recommend that you verify that you are compiling your project against the same target framework as your previous client.

You can do this by right-clicking on your project and selecting Target framework under the Application tab.

Edit: It might be worth taking a screenshot of your current references and uploading it here at stackoverflow. It would give us an better overview of potential issues or conflicts.

enter image description here

There should be no need to modify your App.config, but as a reference here you have mine.

<?xml version="1.0"?>
<configuration>
    <startup> 
    <supportedRuntime version="v2.0.50727"/></startup>
</configuration>

Edit2:

Add the System.dll reference.

  1. Right click on your project and choose Add Reference.
  2. Find and add System (or System.dll) under Assemblies and Framework.
  3. Click OK to save.
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • Maybe I should do something with app.config file in addition. I just added references with right click on the Reference on Solution Explorer and "Add Reference". – gthacoder Mar 22 '13 at 02:17
  • I tried to change framework from my "Framework 4.5" to 4.0, 3.0. It's still the same error. – gthacoder Mar 22 '13 at 02:17
  • Maybe try uploading a screenshot of your references. It could provide us with better insight. – eandersson Mar 22 '13 at 02:22
  • You shouldn't need to make any modifications in the App.config, but I'll include mine for reference. – eandersson Mar 22 '13 at 02:23
  • Screenshot: https://docs.google.com/file/d/0B18cSIbjPPomdHFsdGNEdzZaYjg/edit?usp=sharing – gthacoder Mar 22 '13 at 02:42
  • You are missing `System.dll`. – eandersson Mar 22 '13 at 02:45
  • Hehe, no worries. I included instructions in my answer if needed. – eandersson Mar 22 '13 at 02:48
  • Using VS 2013 and targetting .net 4.0 I am getting the same error. The type of project is "C# Portable library". When I try to add reference ( manually browsing to the directory ) , I get pop-up error "This component is already referenced by the build system". What else I can attempt? – qqqqq Jan 12 '16 at 23:31
0

I faced the same issue. Following changes solved my problem. Change Target framework to .Net Framework 4 from .Net Framework 4 Client Profile.

Client Profile framework creates many such issues.

Satya
  • 1