Domanda

Sto cercando di sviluppare un'applicazione che invia email, e la nostra rete interna è bloccata piuttosto stretto, quindi non posso trasmettere utilizzando i nostri server di posta interni.

Qualcuno ha mai usato qualcosa come no-ip.com? Ci sono altre alternative?

È stato utile?

Soluzione

Se avete solo bisogno di controllare che le e-mail vengono inviati agli indirizzi corretti e con il contenuto corretto, il modo più semplice è quello di utilizzare una cartella di ricezione modificando il file web.config dell'applicazione o:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory" from="me@myorg.com">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\TestMailDrop"/>
      </smtp>
    </mailSettings>
  </system.net>

Questo si tradurrà in le e-mail di essere creati come file nella directory specificata. È anche possibile caricare i file e verificare come parte di un test di unità.

(Come sottolinea codekaizen, questo può anche essere fatto in codice, se non ti dispiace la modifica del codice / hardcoding la cartella di ricezione e avendo il diverso comportamento in modalità debug / release.)

Altri suggerimenti

È possibile salvare l'e-mail sul disco:

#if DEBUG
smtpClient.PickupDirectoryLocation = "\\Path\\to\\save\\folder";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.Send(msg);
#endif

riffing da @codekaizen, utilizzando AutoFixture.xUnit [che è disponibile come pacchetto di xUnit con quel nome]: -

    [Theory, AutoData]
    public static void ShouldSendWithCorrectValues( string anonymousFrom, string anonymousRecipients, string anonymousSubject, string anonymousBody )
    {
        anonymousFrom += "@b.com";
        anonymousRecipients += "@c.com";

        using ( var tempDir = new TemporaryDirectoryFixture() )
        {
            var capturingSmtpClient = new CapturingSmtpClientFixture( tempDir.DirectoryPath );
            var sut = new EmailSender( capturingSmtpClient.SmtpClient );

            sut.Send( anonymousFrom, anonymousRecipients, anonymousSubject, anonymousBody );
            string expectedSingleFilename = capturingSmtpClient.EnumeratePickedUpFiles().Single();
            var result = File.ReadAllText( expectedSingleFilename );

            Assert.Contains( "From: " + anonymousFrom, result );
            Assert.Contains( "To: " + anonymousRecipients, result );
            Assert.Contains( "Subject: " + anonymousSubject, result );
            Assert.Contains( anonymousBody, result );
        }
    }

CapturingSmtpClientFixture viene utilizzato solo in un contesto di prova -

    class CapturingSmtpClientFixture
    {
        readonly string _path;
        readonly SmtpClient _smtpClient;

        public CapturingSmtpClientFixture( string path )
        {
            _path = path;
            _smtpClient = new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory, PickupDirectoryLocation = _path };
        }

        public SmtpClient SmtpClient
        {
            get { return _smtpClient; }
        }

        public IEnumerable<string> EnumeratePickedUpFiles()
        {
            return Directory.EnumerateFiles( _path );
        }
    }

Tutto quello che dovete fare è quindi assicurarsi che il codice vero e proprio fornisce un SmtpClient che è stato cablato con i parametri appropriati per il server SMTP dal vivo.

(Per completezza, ecco TemporaryDirectoryFixture): -

public class TemporaryDirectoryFixture : IDisposable
{
    readonly string _directoryPath;

    public TemporaryDirectoryFixture()
    {
        string randomDirectoryName = Path.GetFileNameWithoutExtension( Path.GetRandomFileName() );

        _directoryPath = Path.Combine( Path.GetTempPath(), randomDirectoryName );

        Directory.CreateDirectory( DirectoryPath );
    }

    public string DirectoryPath
    {
        get { return _directoryPath; }
    }

    public void Dispose()
    {
        try
        {
            if ( Directory.Exists( _directoryPath ) )
                Directory.Delete( _directoryPath, true );
        }
        catch ( IOException )
        {
            // Give other process a chance to release their handles
            // see http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/1703799#1703799
            Thread.Sleep( 0 );
            try
            {
                Directory.Delete( _directoryPath, true );
            }
            catch
            {
                var longDelayS = 2;
                try
                {
                    // This time we'll have to be _really_ patient
                    Thread.Sleep( TimeSpan.FromSeconds( longDelayS ) );
                    Directory.Delete( _directoryPath, true );
                }
                catch ( Exception ex )
                {
                    throw new Exception( @"Could not delete " + GetType() + @" directory: """ + _directoryPath + @""" due to locking, even after " + longDelayS + " seconds", ex );
                }
            }
        }
    }
}

e un EmailSender scheletro di:

public class EmailSender
{
    readonly SmtpClient _smtpClient;

    public EmailSender( SmtpClient smtpClient )
    {
        if ( smtpClient == null )
            throw new ArgumentNullException( "smtpClient" );

        _smtpClient = smtpClient;
    }

    public void Send( string from, string recipients, string subject, string body )
    {
        _smtpClient.Send( from, recipients, subject, body );
    }
}

La solita risposta è quello di eseguire SMTP locale in IIS, ma è necessario stare attenti a chi si sta inviando a. Potrebbe effettivamente essere meglio inviare al solito server SMTP e indirizzare conti solo all'interno del dominio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top