How to Set SMTP Client in POSTAL MVC not in WEB.Config

二次信任 提交于 2019-12-11 12:40:33

问题


i have a problem when using POSTAL MVC in my Project. My hosting services company requires me to set smtp client config in code not in web config.

How to do that ?

I hope someone can give me a solution

Thank you.


回答1:


I had the same problem. There is no reference on the official Postal documentation, nor an How-to. So here goes one:

  1. Create the SmtpClient instance with the custom configuration
  2. Create an Postal.EmailService instance using the constructor which takes 2 arguments (ViewEngineCollection, Func «SmtpClient»)
  3. Now you can send the email using the custom SmtpClient configuration by calling emailService.

Here goes a complete sample code:

dynamic email = new Email("Example");
email.To = "webninja@example.com";
email.FunnyLink = DB.GetRandomLolcatLink();

SmtpClient client = new SmtpClient("mail.domain.com");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("user@domain.pt", "somepassword");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 25;
client.EnableSsl = false;

Postal.EmailService emailService = new Postal.EmailService(new ViewEngineCollection(), () => client);

emailService.Send(email);


来源:https://stackoverflow.com/questions/30837478/how-to-set-smtp-client-in-postal-mvc-not-in-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!