题
我正在尝试开发一个发送电子邮件的应用程序,但我们的内部网络被锁定得非常严密,所以我无法使用我们的内部邮件服务器进行中继。
有人用过 no-ip.com 之类的东西吗?还有其他选择吗?
解决方案
如果你只需要检查的电子邮件被发送到正确的地址,并用正确的内容,最简单的方法是通过编辑应用程序或web.config文件中使用投递文件夹:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory" from="me@myorg.com">
<specifiedPickupDirectory pickupDirectoryLocation="C:\TestMailDrop"/>
</smtp>
</mailSettings>
</system.net>
这将导致被创建为在指定的目录中的文件的电子邮件。甚至可以然后加载文件,并验证它们作为一个单元测试的一部分。
(如codekaizen指出,这也可以在代码中完成,如果你不介意修改代码/硬编码投递文件夹,并具有行为在调试/释放模式有所不同。)
其他提示
我同意上述...设置您自己的测试 SMTP 服务器,并将其用于您的测试。
以下是一些可帮助您走上正轨的信息:
http://service1.symantec.com/support/ent-gate.nsf/docid/2007241920754398
您可以将电子邮件保存到磁盘:
#if DEBUG
smtpClient.PickupDirectoryLocation = "\\Path\\to\\save\\folder";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.Send(msg);
#endif
这@codekaizen Riffing,使用 AutoFixture.xUnit [这是可以作为由该名称的的xUnit包]: -
[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 );
}
}
所有你需要做的是确保你的实际代码将已经连线了适合于实时SMTP服务器参数的SmtpClient
。
(为了完整起见,这里是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 );
}
}
通常的答案是IIS在本地运行SMTP,但你必须要小心你发送给谁。它实际上可能不如送你平常的SMTP服务器和目标仅占域之内。
不隶属于 StackOverflow