I am using SmtpClient to send an email with an attachment. However for a certain batch we need to somehow save the MailMessage instead of sending them. We are then thinking/hoping to manually upload the messages to the users drafts folder.
我正在使用SmtpClient发送带附件的电子邮件。但是对于某个批处理,我们需要以某种方式保存MailMessage而不是发送它们。然后我们考虑/希望手动将消息上传到用户草稿文件夹。
Is it possible to save these messages with the attachment intact (impossible, I would have thought). Or alternatively upload the messages to a folder in the users account?
是否可以保存这些消息与附件完整(不可能,我会想到)。或者将邮件上传到用户帐户中的文件夹?
If anyone has any experience of this, I'd much appreciate a bit of help or a pointer.
如果有人对此有任何经验,我会非常感谢一些帮助或指针。
5 个解决方案
#1
62
When testing in ASP.NET we save our emails to a folder rather then send them through an email server. Maybe you could change yourweb.config
settings like this for your batch?
在ASP.NET中测试时,我们将电子邮件保存到文件夹,而不是通过电子邮件服务器发送。也许您可以为您的批次更改这样的yourweb.config设置?
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
</smtp>
</mailSettings>
</system.net>
Additional Info:
附加信息:
- MSDN: <specifiedPickupDirectory> Element (Network Settings)
-
MSDN:
元素(网络设置) - Configuring SmtpClient to drop emails in a folder on disk
- 配置SmtpClient以删除磁盘上文件夹中的电子邮件
#2
10
As well as the SpecifiedPickupDirectory
information of the other answers, if you want to ensure your emails are sent to a folder relative to the site root - handy in testing on build servers where you don't know the paths - you can add a quick check in your email sending code:
除了其他答案的SpecifiedPickupDirectory信息,如果您想确保将您的电子邮件发送到相对于站点根目录的文件夹 - 在您不知道路径的构建服务器上进行测试时很方便 - 您可以添加快速检查在您的电子邮件中发送代码
SmtpClient client = new SmtpClient();
...
// Add "~" support for pickupdirectories.
if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && client.PickupDirectoryLocation.StartsWith("~"))
{
string root = AppDomain.CurrentDomain.BaseDirectory;
string pickupRoot = client.PickupDirectoryLocation.Replace("~/", root);
pickupRoot = pickupRoot.Replace("/",@"\");
client.PickupDirectoryLocation = pickupRoot;
}
And your tests will look something like this (make sure you use App_Data so IIS can write to the folder):
你的测试看起来像这样(确保你使用App_Data,所以IIS可以写入文件夹):
// Arrange - get SitePath from AppDomain.Current.BaseDirectory + ..\
string pickupPath = Path.Combine(SitePath, "App_Data", "TempSmtp");
if (!Directory.Exists(pickupPath))
Directory.CreateDirectory(pickupPath);
foreach (string file in Directory.GetFiles(pickupPath, "*.eml"))
{
File.Delete(file);
}
// Act (send some emails)
// Assert
Assert.That(Directory.GetFiles(pickupPath, "*.eml").Count(), Is.EqualTo(1));
#3
7
This can help - Adding Save() functionality to Microsoft.Net.Mail.MailMessage
The main ideia, make an extension to MailMessage ,that by reflection making a save method.
这可以提供帮助 - 将保存()功能添加到Microsoft.Net.Mail.MailMessage主要的ideia,对MailMessage进行扩展,通过反射创建一个save方法。
#4
6
You can configure this with the system.net
setting in your web.config
/ app.config
file.
您可以使用web.config / app.config文件中的system.net设置对其进行配置。
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="mail.mydomain.com" port="25" />
</smtp>
<!-- Use this setting for development
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
</smtp>
-->
</mailSettings>
</system.net>
Also, here's a link with info on migrating from System.Web.Mail
to System.Net.Mail
.
此外,这是一个链接,其中包含从System.Web.Mail迁移到System.Net.Mail的信息。
#5
1
A bug also requires adding as a workaround in some versions of the framework. So the completed version looks like:
错误还需要在框架的某些版本中添加作为变通方法。所以完成的版本看起来像:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>
#1
62
When testing in ASP.NET we save our emails to a folder rather then send them through an email server. Maybe you could change yourweb.config
settings like this for your batch?
在ASP.NET中测试时,我们将电子邮件保存到文件夹,而不是通过电子邮件服务器发送。也许您可以为您的批次更改这样的yourweb.config设置?
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
</smtp>
</mailSettings>
</system.net>
Additional Info:
附加信息:
- MSDN: <specifiedPickupDirectory> Element (Network Settings)
-
MSDN:
元素(网络设置) - Configuring SmtpClient to drop emails in a folder on disk
- 配置SmtpClient以删除磁盘上文件夹中的电子邮件
#2
10
As well as the SpecifiedPickupDirectory
information of the other answers, if you want to ensure your emails are sent to a folder relative to the site root - handy in testing on build servers where you don't know the paths - you can add a quick check in your email sending code:
除了其他答案的SpecifiedPickupDirectory信息,如果您想确保将您的电子邮件发送到相对于站点根目录的文件夹 - 在您不知道路径的构建服务器上进行测试时很方便 - 您可以添加快速检查在您的电子邮件中发送代码
SmtpClient client = new SmtpClient();
...
// Add "~" support for pickupdirectories.
if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && client.PickupDirectoryLocation.StartsWith("~"))
{
string root = AppDomain.CurrentDomain.BaseDirectory;
string pickupRoot = client.PickupDirectoryLocation.Replace("~/", root);
pickupRoot = pickupRoot.Replace("/",@"\");
client.PickupDirectoryLocation = pickupRoot;
}
And your tests will look something like this (make sure you use App_Data so IIS can write to the folder):
你的测试看起来像这样(确保你使用App_Data,所以IIS可以写入文件夹):
// Arrange - get SitePath from AppDomain.Current.BaseDirectory + ..\
string pickupPath = Path.Combine(SitePath, "App_Data", "TempSmtp");
if (!Directory.Exists(pickupPath))
Directory.CreateDirectory(pickupPath);
foreach (string file in Directory.GetFiles(pickupPath, "*.eml"))
{
File.Delete(file);
}
// Act (send some emails)
// Assert
Assert.That(Directory.GetFiles(pickupPath, "*.eml").Count(), Is.EqualTo(1));
#3
7
This can help - Adding Save() functionality to Microsoft.Net.Mail.MailMessage
The main ideia, make an extension to MailMessage ,that by reflection making a save method.
这可以提供帮助 - 将保存()功能添加到Microsoft.Net.Mail.MailMessage主要的ideia,对MailMessage进行扩展,通过反射创建一个save方法。
#4
6
You can configure this with the system.net
setting in your web.config
/ app.config
file.
您可以使用web.config / app.config文件中的system.net设置对其进行配置。
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="mail.mydomain.com" port="25" />
</smtp>
<!-- Use this setting for development
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
</smtp>
-->
</mailSettings>
</system.net>
Also, here's a link with info on migrating from System.Web.Mail
to System.Net.Mail
.
此外,这是一个链接,其中包含从System.Web.Mail迁移到System.Net.Mail的信息。
#5
1
A bug also requires adding as a workaround in some versions of the framework. So the completed version looks like:
错误还需要在框架的某些版本中添加作为变通方法。所以完成的版本看起来像:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>