Setting multiple SMTP settings in web.config?

前端 未结 8 1362
庸人自扰
庸人自扰 2020-12-01 00:42

I am building an app that needs to dynamically/programatically know of and use different SMTP settings when sending email.

I\'m used to using the system.net/mailSet

8条回答
  •  悲&欢浪女
    2020-12-01 01:31

    This may or may not help someone but in case you got here looking for Mandrill setup for multiple smtp configurations I ended up creating a class that inherits from the SmtpClient class following this persons code here which is really nice: https://github.com/iurisilvio/mandrill-smtp.NET

        /// 
    /// Overrides the default SMTP Client class to go ahead and default the host and port to Mandrills goodies.
    /// 
    public class MandrillSmtpClient : SmtpClient
    {
    
        public MandrillSmtpClient( string smtpUsername, string apiKey, string host = "smtp.mandrillapp.com", int port = 587 )
            : base( host, port )
        {
    
            this.Credentials = new NetworkCredential( smtpUsername, apiKey );
    
            this.EnableSsl = true;
        }
    }
    

    Here's an example of how to call this:

            [Test]
        public void SendMandrillTaggedEmail()
        {
    
            string SMTPUsername = _config( "MandrillSMTP_Username" );
            string APIKey = _config( "MandrillSMTP_Password" );
    
            using( var client = new MandrillSmtpClient( SMTPUsername, APIKey ) ) {
    
                MandrillMailMessage message = new MandrillMailMessage() 
                { 
                    From = new MailAddress( _config( "FromEMail" ) ) 
                };
    
                string to = _config( "ValidToEmail" );
    
                message.To.Add( to );
    
                message.MandrillHeader.PreserveRecipients = false;
    
                message.MandrillHeader.Tracks.Add( ETrack.opens );
                message.MandrillHeader.Tracks.Add( ETrack.clicks_all );
    
                message.MandrillHeader.Tags.Add( "NewsLetterSignup" );
                message.MandrillHeader.Tags.Add( "InTrial" );
                message.MandrillHeader.Tags.Add( "FreeContest" );
    
    
                message.Subject = "Test message 3";
    
                message.Body = "love, love, love";
    
                client.Send( message );
            }
        }
    

提交回复
热议问题