2

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.

2 Answers2

7

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);
ProfK
  • 49,207
  • 121
  • 399
  • 775
Rafael Lourenço
  • 106
  • 1
  • 10
  • `ViewEngineCollection` is not a valid parameter to that ctor, and you do nothing to explain what it's supposed to be. – ProfK May 08 '16 at 17:25
  • I don't know what ctor means? I have the above code working on a MVC 5 Controller class – Rafael Lourenço May 13 '16 at 18:58
  • ctor is a well known abbreviation for constructor. And, my apologies, I don't know why it didn't accept it earlier, but it did now. You are just short of a `new`, which I have added for you. – ProfK May 14 '16 at 05:02
  • My bad. I think I never saw that abbreviation. Thanks for your editing. – Rafael Lourenço May 17 '16 at 06:07
  • A little late to the party, but I've used this code and get the error: "The parameter 'addresses' cannot be an empty string.". Has something changed with this approach or is there something I'm missing? – Brad Mc Aug 23 '16 at 18:12
  • 3
    EmailService emaiService = new EmailService(ViewEngines.Engines, () => client);emaiService.SendAsync(email); – Houssam Hamdan Apr 06 '17 at 16:37
  • It shows me this error -> "Email view not found for WeeklyEmail. Locations searched:" ... That view is WeeklyEmail is working well when I use it directly through the web.config settings. But it can't find it this way. It doesn't even list anything in the locations searched! – Amitesh Apr 20 '18 at 20:11
  • @Amitesh im getting the same error. were you able to resolve it?? – sanjeev Nov 01 '18 at 16:26
0

To Add to Rafael's answer. If you use

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

you might get this error: Email view not found for [name for he email template]. Locations searched because new Postal.EmailService(new ViewEngineCollection(), () => client) creates emailService with empty view engine. Use ViewEngines.Engines inplace of new ViewEngineCollection() (as sugested by @Houssam Hamdan in comment above) to use razor view engine.

Complete line:

Postal.EmailService emailService = new Postal.EmailService(ViewEngines.Engines, () => client);
iAM
  • 1,365
  • 15
  • 25