我正在尝试开发一个发送电子邮件的应用程序,我们的内部网络被锁定得很紧,所以我无法使用我们的内部邮件服务器进行中继。
有没有人用过 no-ip.com 之类的东西?还有其他选择吗?
我正在尝试开发一个发送电子邮件的应用程序,我们的内部网络被锁定得很紧,所以我无法使用我们的内部邮件服务器进行中继。
有没有人用过 no-ip.com 之类的东西?还有其他选择吗?
如果您只需要检查电子邮件是否发送到正确的地址和正确的内容,最简单的方法是通过编辑 app 或 web.config 文件来使用 drop 文件夹:
<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
使用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 );
}
}
通常的答案是在 IIS 下本地运行 SMTP,但您需要小心发送给谁。实际上,最好发送到您常用的 SMTP 服务器并仅针对您域中的帐户。