Search code examples
asp.net-mvc-3smtpclientmvcmailer

How set up different stmpclient instances in web.config?


I don't believe this is specifically an MvcMailer question (this is the mailer I am using), but I am struggling with framing a Googleplex search to figure out how to send e-mails from different accounts based on my context.

I have a need to send two e-mails from two different e-mail accounts. I have tried using

mailMessage.From = new MailAddress("some-other-email@gmail.com");

in MvcMailer, but that doesn't even show up in the e-mail I dump to the temp directory. It shows up as what is in the web.config: "some-email@gmail.com".

This is my web.config for MvcMailer:

<mailSettings>
      <!-- Method#1: Configure smtp server credentials -->
      <!--<smtp from="some-email@gmail.com">
        <network enableSsl="true" host="smtp.gmail.com" port="587" userName="some-email@gmail.com" password="valid-password" />
      </smtp>-->
      <!-- Method#2: Dump emails to a local directory -->

            <smtp from="some-email@gmail.com" deliveryMethod="SpecifiedPickupDirectory">
                <network host="localhost" />
                <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\" />
            </smtp>

    </mailSettings>

This is the mailer code:

public virtual MailMessage EMailConsultation(EMailConsultationData model)
        {
            var mailMessage = new MailMessage { Subject = "INQUIRY: E-Mail Consultation" };

            mailMessage.From = new MailAddress("some-other-email@gmail.com");//I tested this to see if at the very least it would show up in the e-mail, but it didn't.

            mailMessage.To.Add(model.EMail);

            ViewData = new ViewDataDictionary(model);
            PopulateBody(mailMessage, viewName: "InquiryEMailConsultation");

            return mailMessage;
        }

Again, the above code works to send e-mail. I just do not know how I can set up the mailer to send from a specified e-mail address, rather than just from "some-email@gmail.com" as is in the web.config. I have multiple MailMessages, and have a need to send certain ones from a different e-mail account.

I would greatly appreciate any help/code examples.


Solution

  • You could create your own SmtpClient object in the code and send your generated email with that. And only use 1 smtp setting in the web.config (the default one).

    in your web.config for MvcMailer:

    <mailSettings>
        <smtp from="some-email@gmail.com" deliveryMethod="SpecifiedPickupDirectory">
            <network host="localhost" />
            <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\" />
        </smtp>
    </mailSettings>
    

    and use MyMailer.EMailConsultation().Send();

    if you need to send an email via google (eg) use this:

    using (var googleSmtp = new SmtpClient("smtp.gmail.com", 587))
    {
        googleSmtp.EnableSsl = true;
        googleSmtp.Credentials = new NetworkCredential("some-email@gmail.com", "valid-password");
    
        googleSmtp.Send(MyMailer.EMailConsultation());
    }