在整个应用程序中实现电子邮件功能的最佳方法是什么? [英] What is the best way to implement Email functionaity across the application?

查看:142
本文介绍了在整个应用程序中实现电子邮件功能的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个从各个地方向用户发送电子邮件的功能

喜欢注册,更改密码等。

需要根据情况通过不同的参赛者比如电子邮件语言格式或电子邮件或加密密码中的GotoURL。

有人告诉我应该是代码可维护性,可控性,可重用性的最佳功能。

TIA

I want to create a function which sends email to the user from various places
like registeration, change password etc.
It requires to pass different Paramaters according to situation like Email language format or GotoURL in body of email or encrypted password.
can someone tell me should be the best possible function for code maintainability , scalibilty, reusibility.
TIA

推荐答案

拥有一个电子邮件助手类,其中包含您要发送的每种邮件类型的特定方法。这允许您只发送您想要的参数,并在电子邮件相关的类中包含此电子邮件逻辑。



Have an Email helper class that contains a specific method for each type of mail you want to send. This allows you to send just the params you want and also contain this email logic in an email-related class.

public static class EmailHelper
{
    public static void SendRegistrationMail(User user)
    {
        // here I have a User class that the calling code creates.  It has things like the user's nanme, email etc

        // This method is only for registration so we construct the aspects of the mail specific to that
        // we can pass extra params into this function if we want, ot get this data from settings etc

        MailAddress from = new MailAddress("admin@mysite.com", "My Site");
        MailAddress to = new MailAddress(user.Email, user.Name);

        string subject = "Thanks for registering";
        string body = string.Format("Thanks for registering with our site {0}", user.Name);

        // now we've got the data for our registration email we can send it using the generic internal function

        SendEmail(from, to, subject, body);
    }

    public static void SendOrderMail(Basket basket)
    {
        // here I have a Basket class that the calling code creates.  It has things to do with the basket and also a User
        // property

        // This method is only for orders so we construct the aspects of the mail specific to that
        // we can pass extra params into this function if we want, ot get this data from settings etc

        MailAddress from = new MailAddress("admin@mysite.com", "My Site");
        MailAddress to = new MailAddress(basket.User.Email, basket.User.Name);

        string subject = "Thanks for your order";
            
        // construct the body from the Basket object to include the items they have ordered etc
        string body = "Order Summary: ...";

        // now we've got the data for our order email we can send it using the generic internal function

        SendEmail(from, to, subject, body);
    }

    private static void SendEmail (MailAddress from, MailAddress to, string subject, string body)
    {
        // send the email in here
    }
}





来电代码





Calling code

User user = new User();

user.Email = ".. from form";
user.Name = ".. from form";

EmailHelper.SendRegistrationMail(user);


ASP.NET MVC为您提供了控制器,尝试向您的应用程序添加控制器管理电子邮件。例如,



ASP.NET MVC has provided you with Controllers, try adding a controller to your application that manages the Emails. Such as,

public class EmailController : Controller 
{
   // Create private and public members for to and from etc. 
   public ActionResult Send(object parameter)
   {
       // Change object to any type, a model or form etc.
   }
   // ... Rest of stuff
}





现在,这可以从任何页面使用。只需将表单提交到 / email / 位置,控制器就可以将电子邮件发送给客户。



好​​的是你会为这个控制器提供单独的视图,你可能想要使用一个模型(它会保留,电子邮件的详细信息。如To,From,Subject,Body等。然后,您可以将该模型传递给Send动作,并且操作可以使用Model对象中的成员字段发送电子邮件。通过这种方式,您可以设置一些您正在谈论的参数,电子邮件语言,GotoUrl等。



无论如何,您还必须考虑保留密码安全的地方,不要暴露给公众。



Now, this can be use from any page. Just submit the form to your /email/ location and controller will be able to send the email to client.

Good thing is that you would be provided with separate Views for this controller and you may want to use a Model (which would hold, details for the email. Such as To, From, Subject, Body etc.) Then you can pass that model to Send action and action may send the email by using the member fields in the Model object. This way, you will be able to set up some parameters the one you are talking about, Email language, GotoUrl etc.

Anyways, you must also consider keeping the passwords safe somewhere, do not expose them to the public.


如果你想要它漂亮,功能齐全,随处可用,这就是我要做的:



1.创建一个用于发送电子邮件的主私人基本功能。

2.为每种类型的电子邮件创建一个私有函数,该函数将使用正确的参数调用基本函数。

3.创建一个包含电子邮件类型的枚举。

4.创建一个公共静态函数,该函数将带有参数以及Enum参数(来自数字3),该参数将根据Enum调用正确的私有函数,并将该函数放在一个静态类中,该类将在任何命名空间之外......类似于NativeMethods等...



*此外,我将创建基本电子邮件功能以在Ta中工作sk,所以如果我花很长时间它不会冻结主线程。
if you want it to be pretty, functional and available everywhere, this is what I would do:

1. Create a main private base function for sending email.
2. Create a private function for each type of email that will invoke the base function with the right parameters.
3. Create an Enum with email types.
4. Create a public static function that will take the parameters along with Enum parameter (from number 3) which will according to the Enum invoke the right private function, and put that function in a static class that will be outside of any namespace... something like NativeMethods etc...

*Also I would create the base email function to work in a Task, so if I takes long time it won't freeze the main thread.


这篇关于在整个应用程序中实现电子邮件功能的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆