1

電子メールを送信するアプリを開発しようとしていますが、内部ネットワークが非常に厳重にロックされているため、内部メール サーバーを使用して中継することはできません。

no-ip.com のようなものを使用したことのある人はいますか? 他の選択肢はありますか?

4

5 に答える 5

4

電子メールが正しいアドレスに正しい内容で送信されていることを確認するだけの場合は、アプリまたは web.config ファイルを編集してドロップ フォルダーを使用するのが最も簡単な方法です。

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

これにより、指定したディレクトリに電子メールがファイルとして作成されます。その後、ファイルをロードして単体テストの一部として検証することもできます。

(codekaizen が指摘しているように、コードを変更する/ドロップ フォルダーをハードコーディングし、デバッグ/リリース モードで動作が異なることを気にしない場合は、これをコードで行うこともできます。)

于 2010-02-04T18:17:44.483 に答える
2

上記に同意します...独自のテストSMTPサーバーをセットアップし、それをテストに使用します。

正しい軌道に乗るための情報を次に示します。

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/e4cf06f5-9a36-474b-ba78-3f287a2b88f2.mspx?mfr=true

http://www.cmsconnect.com/praetor/WebHelpG2/Chapter_2_-_Pre-installation_considerations/Configuring_the_SMTP_Server.htm

http://service1.symantec.com/support/ent-gate.nsf/docid/2007241920754398

于 2010-02-04T18:07:53.600 に答える
2

電子メールをディスクに保存できます。

#if DEBUG
smtpClient.PickupDirectoryLocation = "\\Path\\to\\save\\folder";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.Send(msg);
#endif
于 2010-02-04T18:16:40.583 に答える
1

AutoFixture.xUnit [その名前で XUnit パッケージとして利用可能] を使用して @codekaizen からリフ:-

    [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テストコンテキストでのみ使用されます-

    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 );
        }
    }

次に行う必要があるのは、実際のコードSmtpClientがライブ SMTP サーバーに適切なパラメーターで接続された を提供していることを確認することだけです。

(完全を期すために、ここにあります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 );
                }
            }
        }
    }
}

とスケルトンEmailSender:

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 );
    }
}
于 2012-08-10T11:30:34.773 に答える
0

通常の答えは、SMTP を IIS の下でローカルに実行することですが、送信先に注意する必要があります。実際には、通常の SMTP サーバーに送信して、ドメイン内のアカウントのみをターゲットにする方がよい場合があります。

于 2010-02-04T18:01:40.447 に答える