Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标准邮件收发类?_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 标准邮件收发类?

C# 标准邮件收发类?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在浏览我的应用程序,试图清理一些发送电子邮件的代码。我开始创建自己的emailer包装类,但后来我发现一定有一个标准的emailer类。我做了一些搜索,但什么也找不到 还有,像这样的东西在任何地方都有代码库吗 编辑:对不起,让我澄清一下 在我需要发送电子邮件时,我不想在代码中包含此内容: System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage(); message.From="from e-mail"; mess

我正在浏览我的应用程序,试图清理一些发送电子邮件的代码。我开始创建自己的emailer包装类,但后来我发现一定有一个标准的emailer类。我做了一些搜索,但什么也找不到

还有,像这样的东西在任何地方都有代码库吗

编辑:对不起,让我澄清一下

在我需要发送电子邮件时,我不想在代码中包含此内容:

System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
Emailer.SendEmail("name@site.com", "name2@site.com", "My e-mail", false);
我创建了一个名为Emailer的类,该类包含以下函数:

SendEmail(string to, string from, string body)
SendEmail(string to, string from, string body, bool isHtml)
所以我可以在我的代码中只写一行来发送电子邮件:

System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
Emailer.SendEmail("name@site.com", "name2@site.com", "My e-mail", false);
我的意思是,这并不太复杂,但我认为有一个标准的、公认的解决方案。

也许你在寻找这样的解决方案

using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using MailMessage=System.Net.Mail.MailMessage;

class CTEmailSender
{
    string MailSmtpHost { get; set; }
    int MailSmtpPort { get; set; }
    string MailSmtpUsername { get; set; }
    string MailSmtpPassword { get; set; }
    string MailFrom { get; set; }

    public bool SendEmail(string to, string subject, string body)
    {
        MailMessage mail = new MailMessage(MailFrom, to, subject, body);
        var alternameView = AlternateView.CreateAlternateViewFromString(body, new ContentType("text/html"));
        mail.AlternateViews.Add(alternameView);

        var smtpClient = new SmtpClient(MailSmtpHost, MailSmtpPort);
        smtpClient.Credentials = new NetworkCredential(MailSmtpUsername, MailSmtpPassword);
        try
        {
            smtpClient.Send(mail);
        }
        catch (Exception e)
        {
            //Log error here
            return false;
        }

        return true;
    }
}

我使用的是一个通用类,由 试试这个



这是我的一个项目中的一个片段。与其他一些实现相比,它的功能更加紧凑

使用此功能可以创建包含以下内容的电子邮件:

  • 可在运行时用实际值替换的标记
  • 同时包含文本和HTML视图的电子邮件

    public MailMessage createMailMessage(string toAddress, string fromAddress, string subject, string template)
    {
    // Validate arguments here...
    
    // If your template contains any of the following {tokens}
    // they will be replaced with the values you set here.
    var replacementDictionary = new ListDictionary
           {
               // Replace with your own list of values
               { "{first}", "Pull from db or config" },
               { "{last}", "Pull from db or config" }
           };
    
    // Create a text view and HTML view (both will be in the same email)
    // This snippet assumes you are using ASP.NET (works w/MVC)
    //    if not, replace HostingEnvironment.MapPath with your own path.
    var mailDefinition = new MailDefinition { BodyFileName = HostingEnvironment.MapPath(template + ".txt"), IsBodyHtml = false };
    var htmlMailDefinition = new MailDefinition { BodyFileName = HostingEnvironment.MapPath(template + ".htm"), IsBodyHtml = true };
    
    MailMessage htmlMessage = htmlMailDefinition.CreateMailMessage(email, replacementDictionary, new Control());
    MailMessage textMessage = mailDefinition.CreateMailMessage(email, replacementDictionary, new Control());
    
    AlternateView plainView = AlternateView.CreateAlternateViewFromString(textMessage.Body, null, "text/plain");
    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlMessage.Body, null, "text/html");
    
    var message = new MailMessage { From = new MailAddress(from) };
    
    message.To.Add(new MailAddress(toAddress));
    message.Subject = subject;
    
    message.AlternateViews.Add(plainView);
    message.AlternateViews.Add(htmlView);
    
    return message;
    }
    
假设您已经为NetMail设置了Web.config,您可以从助手方法调用此方法,如下所示:

public bool SendEmail(MailMessage email)
{
    var client = new SmtpClient();

    try
    {
       client.Send(message);
    }
    catch (Exception e)
    {
        return false;
    }

    return true;
}

SendMail(createMailMessage("to@email.com", "from@email.com", "Subject", "~/Path/Template"));

请澄清。你所说的“标准邮件收发类”是什么意思?将表单字段转换为电子邮件的东西?@anon同意。我想OP不仅仅是询问?@Kirk Woll事实上,我觉得他是。我想他希望有一个发送电子邮件的服务层。@Shawn-是的,这就是我的意思。:)抱歉,我很难把我需要的东西用语言表达出来。是的,谢谢。你是从哪个代码库得到这个的,或者你知道有哪个代码库有这样的代码服务吗?@Steven:我刚从我的项目代码中选了一门课:)70-536培训手册有一个关于发送电子邮件的好章节。听起来可能像是个新手问题。“using MailMessage=System.Net.Mail.MailMessage;”这句话是什么意思?您的代码应该可以在没有它的情况下运行,因为您已经导入了System.Net.Mail。@Jonas Everest:我必须承认,这里不再需要它了。当您在两个或多个不同的名称空间中有两个名为
MailMessage
的类,并且您希望指出,此文件中的
MailMessage
指向System.Net.Mail.MailMessage时,将使用此结构。我相信.NET在不同的命名空间中有
MailMessage
类,但它已被弃用。