Skip to content
Snippets Groups Projects
F

Fluent Mailer

Project ID: 915503
Select Git revision
  • dev
  • release/1.1.5
  • master default protected
  • 1.1.4
  • 1.1.3
  • 1.1.2
  • 1.1.1
  • 1.1
  • 1.0.9
  • 1.0.8
  • 1.0.7
  • 1.0.6
  • 1.0.4
  • 1.0.2
  • 1.0.0
15 results
  • Clone with SSH
  • Clone with HTTPS
  • user avatar
    Леонов Артем authored
    2eab46a0
    History

    Fluent Mailer

    Module for sending emails via smtp with fluent interface

    Configuration

    Add into Web.config/app.config standart system.net/mailSettings configuration.

    For example:

    <system.net>
    	<mailSettings>
    		<smtp from="qwerty@qwerty.com">
    			<network enableSsl="true" host="smtp.qwerty.com" port="587" userName="qwerty@qwerty.com" password="qwertyqwerty" />
    		</smtp>
    	</mailSettings>
    </system.net>

    Prepare Fluent Mailer Resolving

    For now you have two ways to resolve Fluent Mailer Instance:

    1. Register Fluent Mailer dependencies in Unity (using FluentMailer.Unity package)
    var unityContainer = new UnityContainer();
    unityContainer.RegisterFluentMailerDependencies();

    Now, you can resolve Fluent Mailer through Unity:

    public class SomeService
    {
        private readonly IFluentMailer _fluentMailer;
      
        public SomeService(IFluentMailer fluentMailer)
        {
            _fluentMailer = fluentMailer;
        }
      
        public void SendMessage()
        {
            // Using _fluentMailer here
        }
    }
    1. Resolve Fluent Mailer through a static factory (using FluentMailer.Factory package)
    var fluentMailer = FluentMailerFactory.Create();

    Additionally, you can configure resolving Fluent Mailer through a static factory for you dependency injection container. Example for NInject:

    Bind<IFluentMailer>.ToMethod(context => FluentMailerFactory.Create())

    Using Fluent Mailer

    1. Create Message

    var message = _fluentMailer.CreateMessage();

    2. Configure Message Body

    With view

    var mailSender = message.WithView("~/Views/Mailer/Mail.cshtml");

    With view and model

    var model = new MailModel();
    var mailSender = message.WithView("~/Views/Mailer/Mail.cshtml", model);

    With view body

    var mailSender = message.WithViewBody("<html><body>Test message</body></html>");

    3. Configure Other Message Properties

    Adding receivers

    mailSender.WithReceiver("abc@abc.com"); // Adds abc@abc.com to recievers
    mailSender.WithReceivers(new [] {"bcd@bcd.com", "cde@cde.com"}); // Adds bcd@bcd.com and cde@cde.com to receivers too
    mailSender.WithReceivers("def@def.com", "efg@efg.com"); // Finally, adds def@def.com and efg@efg.com to receivers

    Setting Up Subject

    mailSender.WithSubject("Mail subject");

    Adding attachments

    var fstream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/1.docx", FileMode.Open);
    mailSender.WithAttachment(fstream, "document.docx")
    	.WithAttachment("~/book.pdf");

    4. Send Mail

    Synchronously

    mailSender.Send();

    Asynchronously

    await mailSender.SendAsync();

    Fluent Interface

    You can do all of it in one move

    await _fluentMailer.CreateMessage()
    	.WithViewBody("<html><body>Test message</body></html>")
    	.WithReceivers("abc@abc.com", "bcd@bcd.com")
    	.WithSubject("Mail subject")
    	.WithAttachment("~/book.pdf")
    	.SendAsync();