How to configure various stmpclient instances in web.config?

I don’t think this is an MvcMailer question (this is the email program I use), but I am struggling with creating a Googleplex search to figure out how to send emails from different accounts based on my context.

I need to send two email messages from two different email accounts. I tried using

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

in MvcMailer, but it doesn't even appear in the email inbox in the temp directory. It displays as what is in the web.config file: " 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 email. I just don’t know how to configure the mailbox to be sent from the specified email address, and not just from " some-email@gmail.com ", as in web.config. I have several MailMessages, and you need to send specific messages from another email account.

I would really appreciate any help / code examples.

+4
source share
2 answers

You can create your own SmtpClient object in code and send it with a generated email. And use only 1 smtp setting in web.config file (default).

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 (for example) 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()); } 
+5
source

Alternatively, you can create your own custom web configuration section to handle multiple entries with a backup and all. See http://www.coralys.com/Articles/Custom-ASPNET-Config-Sections.aspx

0
source

Source: https://habr.com/ru/post/1394625/


All Articles