codedom添加参考

时间:2012-08-15 21:09:10

标签: c# reference codedom mail-sender

我想制作邮件发件人生成器。但我有编码参考的问题。我的结果是: “类型或命名空间名称'Mail'在名称空间'System.Net'中不存在(您是否缺少程序集引用?)”

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

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

1 个答案:

答案 0 :(得分:1)

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

相关问题