معرفی کتابخانه Postal برای ASP.NET MVC
نویسنده: آرمین ضیاء
تاریخ: ۱۳۹۲/۱۰/۲۰ ۰:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
PM> Install-Package Postal
using Postal;
public class HomeController : Controller
{
public ActionResult Index()
{
dynamic email = new Email("Example");
email.To = "webninja@example.com";
email.FunnyLink = DB.GetRandomLolcatLink();
email.Send();
return View();
}
} To: @ViewBag.To From: lolcats@website.com Subject: Important Message Hello, You wanted important web links right? Check out this: @ViewBag.FunnyLink <3
<configuration>
...
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="example.org" port="25" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
...
</configuration> namespace App.Models
{
public class ExampleEmail : Email
{
public string To { get; set; }
public string Message { get; set; }
}
} public void Send()
{
var email = new ExampleEmail
{
To = "hello@world.com",
Message = "Strong typed message"
};
email.Send();
} @model App.Models.ExampleEmail To: @Model.To From: postal@example.com Subject: Example Hello, @Model.Message Thanks!
public class ExampleController : Controller
{
public ExampleController(IEmailService emailService)
{
this.emailService = emailService;
}
readonly IEmailService emailService;
public ActionResult Index()
{
dynamic email = new Email("Example");
// ...
emailService.Send(email);
return View();
}
} [Test]
public void ItSendsEmail()
{
var emailService = A.Fake<IEmailService>();
var controller = new ExampleController(emailService);
controller.Index();
A.CallTo(() => emailService.Send(A<Email>._))
.MustHaveHappened();
} To: test@test.com From: example@test.com Subject: Fancy email Views: Text, Html
Content-Type: text/plain; charset=utf-8 Hello @ViewBag.PersonName, This is a message
Content-Type: text/html; charset=utf-8
<html>
<body>
<p>Hello @ViewBag.PersonName,</p>
<p>This is a message</p>
</body>
</html> dynamic email = new Email("Example");
email.Attach(new Attachment("c:\\attachment.txt"));
email.Send(); <configuration>
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="Postal" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration> To: john@example.org
From: app@example.org
Subject: Image
@Html.EmbedImage("~/content/postal.jpg") using Postal;
class Program
{
static void Main(string[] args)
{
// Get the path to the directory containing views
var viewsPath = Path.GetFullPath(@"..\..\Views");
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var service = new EmailService(engines);
dynamic email = new Email("Test");
// Will look for Test.cshtml or Test.vbhtml in Views directory.
email.Message = "Hello, non-asp.net world!";
service.Send(email);
}
} Email Headers: برای در بر داشتن نام، در آدرس ایمیل از فرمت زیر استفاده کنید.
To: John Smith <john@example.org>
Bcc: john@smith.com, harry@green.com Subject: Example etc
Bcc: john@smith.com Bcc: harry@green.com Subject: Example etc
public class ExampleController : Controller
{
public ExampleController(IEmailService emailService)
{
this.emailService = emailService;
}
readonly IEmailService emailService;
public ActionResult Index()
{
dynamic email = new Email("Example");
// ...
var message = emailService.CreateMailMessage(email);
CustomProcessMailMessage(message);
return View();
}
} Install-Package Postal.Mvc4
PM> Install-Package Postal.Mvc5
No default Instance is registered and cannot be automatically determined for type 'Postal.IEmailViewRenderer' There is no configuration specified for Postal.IEmailViewRenderer 1.) new EmailService(*Default of IEmailViewRenderer*, *Default of IEmailParser*, *Default of Func<SmtpClient>*) 2.) Postal.EmailService 3.) Instance of Postal.IEmailService (Postal.EmailService) 4.) new AccountController(*Default of IApplicationUserManager*, *Default of IApplicationSignInManager*, *Default of IAuthenticationManager*, *Default of IProfileService*, *Default of IUserService*, *Default of IIdentityMessageService*, *Default of IEmailService*, *Default of IUnitOfWork*) 5.) Annual_faculty_promotions.WebUI.Controllers.AccountController 6.) Instance of Annual_faculty_promotions.WebUI.Controllers.AccountController 7.) Container.GetInstance(Annual_faculty_promotions.WebUI.Controllers.AccountController)
ioc.For<IEmailService>().Use<EmailService>();
این وابستگیها رو باید تنظیم کنی
IEmailService, EmailService
IEmailViewRenderer, EmailViewRenderer
IEmailParser, EmailParser
A from address must be specified
<smtp deliveryMethod="Network" from="xxx@gmail.com">
<network host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="xxx@gmail.com"
password="xxx" />
</smtp> <smtp deliveryMethod="Network" from="xxx">
<network host="smtp.gmail.com" password="yyy"
port="587" userName="zzz" enableSsl="true" />
</smtp>