首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >codedom添加引用

codedom添加引用
EN

Stack Overflow用户
提问于 2012-08-16 05:09:10
回答 1查看 3.1K关注 0票数 0

我想做一个邮件发送器生成器。但是我对codedom引用有问题。我的结果是:“类型或命名空间名称'Mail‘在命名空间'System.Net’中不存在(您是否缺少程序集引用?)”

我不能添加system.net.mail,因为system.net.mail不是dll文件。我的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Runtime.InteropServices;

namespace compilerTest
{
    class Program
    {
        static void compileIt1()
        {
            //mail sender code
            string source =
            @"
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace mail_sender
{
    class Program
    {
        static void Main(string[] args)
        {

           SmtpClient smtp = new SmtpClient(""smtp.gmail.com"", 587);

            NetworkCredential myCredentials = new NetworkCredential(""sendermail@gmail.com"", ""password"");

            smtp.Credentials = myCredentials;

            smtp.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
            smtp.Send(""sendermail@gmail.com"", ""tomaill@gmail.com"", ""titel"", ""mail body"");
            Console.ReadKey();
        }

    }

}
           ";

            //version
            Dictionary<string, string> providerOptions = new Dictionary<string, string>
                {
                    {"CompilerVersion", "v3.5"}
                };
            //code type
            CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);

            //output file
            CompilerParameters compilerParams = new CompilerParameters
            {
                OutputAssembly = "D:\\mailsender.EXE",
                GenerateExecutable = true
            };
            compilerParams.ReferencedAssemblies.Add("System.Net.Dll");

            //compile
            CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);

            //errors
            Console.WriteLine("Number of Errors: {0}", results.Errors.Count);
            foreach (CompilerError err in results.Errors)
            {
                Console.WriteLine("ERROR {0}", err.ErrorText);
            }
        }

        static void Main(string[] args)
        {
            compileIt1();
            Console.WriteLine("Press a key...");
            Console.ReadKey();
        }
    }
}

我有:.net 3.5 VS 2010

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-16 07:39:43

SmtpClient等人。在System程序集中定义,而不是在System.Net中定义。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11977342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档