0

I would like to use multiple sender's email (smtp) for different cases with actionmailer MVC.

For example, if it's a new user registering, then the confirmation will be sent with the register@example.com email.

If the user get contact by another user, the sender email will be contact@example.com.

So I need to setup 3-4 smtp, and use them in actionmailer. So far, webconfig can't support multiple smtp. Thks

Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
alex
  • 55
  • 1
  • 13

1 Answers1

1

MailerBase has a property From available (among others) that you can set in C# depending on whatever logic you use. Then combine that with <appSettings> from the web.config you can do something like:

<appSettings>
    <add key="RegistrationFromAddress" value="register@example.com" />
    <add key="ContactFromAddress" value="contact@example.com" />
</appSettings>

then in your controller

public class MailController : MailerBase
{
    public EmailResult RegisterEmail()
    {
        From = System.Configuration.ConfigurationManager.AppSettings["RegistrationFrom"]; // or ContactFromAddress if you want
    }
}
Steven V
  • 16,357
  • 3
  • 63
  • 76
  • For what I understand, you need to set up account in order to send an email. You have to do that using mailSettings/smtp in webconfig. My goal is to setup multiple accounts and pick the right one at the right time. – alex Sep 29 '13 at 01:21
  • @alex Some email providers (especially one's that you pay for, or are apart of a hosting package) you can send multiple From addresses from one approved account. But if you cannot, you're probably implementing your own `IMailSender` where you could change the underlying `SmtpClient`'s settings to what you need to for each account. Take a look at https://bitbucket.org/swaj/actionmailer.net/wiki/Home#!advanced-stuff for more information. – Steven V Sep 29 '13 at 01:31
  • Yes, you are right, some provider offer that. I give you the answer. But my real question was about the ones that does not offer multiple accounts, like hotmail for instance. How can I setup multiple SMTP in web.config? Or how can I use IMailSender to set up multiple sender account (with code) ? – alex Oct 01 '13 at 04:56