c#不同的邮件正文条件

本文关键字:正文 条件 | 更新日期: 2023-09-27 18:14:57

我有一个带有两个单选按钮的表单,根据检查哪个表单,将运行不同的进程。我想在任何一个流程完成时发送电子邮件通知,但要让主题和正文指示哪个流程完成。

class SendMail
{
    public void sendMail()
    {
        MailMessage dailyMail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "smtp.google.com";
        dailyMail.Subject = "Daily load done";
        dailyMail.Body = "The daily load process has completed.";
        client.Send(dailyMail);
    }
}

只是使用这个通用的sendMail方法,我应该保留dailyMail对象(一个进程)和创建一个monthlyMail对象(另一个进程),并有相应的对象触发器与适当的单选按钮选择?

是否有更好(读:更简单)的方法来完成这一点?谢谢。

c#不同的邮件正文条件

可以将dailyMail对象传递给方法

class SendMail
{
    public void sendMail(MailMessage dailyMail)
    {
        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "smtp.google.com";
        client.Send(dailyMail);
    }
}

你可以创建dailyMail对象,并在调用方法之前设置主题和主体,例如

var dailyMail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
if(radioButton1.Checked)
{
    dailyMail.Subject = "Daily load done";
    dailyMail.Body = "The daily load process has completed.";
}
else
{
    dailyMail.Subject = "Some other subject";
    dailyMail.Body = "Some other method";
}
var sendMail = new SendMail();
sendMail.sendMail(dailyMail);

在没有看到更多代码的情况下,很难确定方法,但最好创建一个子类继承的基邮件类。这些子类设置了主题/主体/其他任何需要它的东西。

public abstract class MailBase
{
    private SmtpClient _client;
    public string To { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public MailBase()
    {
         _client = new SmtpClient();
         _client.Port = 25;
         _client.DeliveryMethod = SmtpDeliveryMethod.Network;
         _client.UseDefaultCredentials = false;
         _client.Host = "smtp.google.com";
    }
    protected void SendInternal()
    {
        //Send code
    }
}
public class DailyMail : MailBase
{
    public DailyMail(string to)
    {
         To = to;
         From = "someemail@email.com";
         Subject = "My Subject";
         Body = "My body";
    }
    public void Send()
    {
         SendInternal();
    }
}

您可以实例化该类型并调用Send

//Somewhere in code
var dailyMail = new DailyMail("user@email.com");
dailyMail.Send();

此解决方案与其他解决方案类似,但利用了将邮件发送放在静态方法中的优势,因此调用更简洁一些,资源得到了适当的处置。此外,UI/表示代码可以稍微分开一点,以便在将来需要添加更多单选按钮时更容易发送其他邮件消息(或者如果您想在其他类或项目中重用代码)。

public class Mailer
{
    public static void Send(MailMessage mailMessage)
    {
        using (SmtpClient client = new SmtpClient())
        {
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "smtp.google.com";
            client.Send(mailMessage);
        }
    }
}

在UI或表示层代码中:

private void HandleRadioSelection()
{
    if (radioButton1.Checked)
    {
        this.SendMailMessage("Daily load done.", "The daily load process has completed.");
    }
    else
    {
        this.SendMailMessage("Some other subject", "Some other body");
    }
}
private void SendMailMessage(string subject, string body)
{
    using (MailMessage mailMessage = new MailMessage("you@yourcompany.com", "user@hotmail.com", subject, body))
    {
        Mailer.Send(mailMessage);
    }
}