Comments (3)
Based on Microsoft's referencesource, when SmtpDeliveryMethod.SpecifiedPickupDirectory
is used, the SmtpClient saves the message to the specified pickup directory location using a randomly generated filename based on Guid.NewGuid ().ToString () + ".eml"
, so to achieve the same results with MailKit, you could do something like this:
public static void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
{
do {
// Generate a random file name to save the message to.
var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
Stream stream;
try {
// Attempt to create the new file.
stream = File.Open (path, FileMode.CreateNew);
} catch (IOException) {
// If the file already exists, try again with a new Guid.
if (File.Exists (path))
continue;
// Otherwise, fail immediately since it probably means that there is
// no graceful way to recover from this error.
throw;
}
try {
using (stream) {
// IIS pickup directories expect the message to be "byte-stuffed"
// which means that lines beginning with "." need to be escaped
// by adding an extra "." to the beginning of the line.
//
// Use an SmtpDataFilter "byte-stuff" the message as it is written
// to the file stream. This is the same process that an SmtpClient
// would use when sending the message in a `DATA` command.
using (var filtered = new FilteredStream (stream)) {
filtered.Add (new SmtpDataFilter ());
// Make sure to write the message in DOS (<CR><LF>) format.
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
message.WriteTo (options, filtered);
filtered.Flush ();
return;
}
}
} catch {
// An exception here probably means that the disk is full.
//
// Delete the file that was created above so that incomplete files are not
// left behind for IIS to send accidentally.
File.Delete (path);
throw;
}
} while (true);
}
from mailkit.
Excellent. Thanks a lot for this valuable answer!
PS: Wouldn't an information like this be valuable by being added to the MailKit documentation?
from mailkit.
It's in the FAQ: https://github.com/jstedfast/MailKit/blob/master/FAQ.md#smtp-specified-pickup-directory
I couldn't figure out how to grab the link from my phone last night or I would have posted that :-)
from mailkit.
Related Issues (20)
- Unable to delete certain e-mails - Index was outside the bounds of the array. HOT 5
- MailKit.Net.Pop3.Pop3ProtocolException - HResult=0x80131500 Message=Unexpected response from server: HOT 2
- GMail - remove label from mailbox HOT 2
- `Sender` from a gmail group alias doesn't work? HOT 4
- Retrieving a list of all subfolders returns only subscribed subfolders HOT 6
- Exception on IMapFolder.FetchAsync HOT 4
- ImapClient CountChange USE HOT 5
- Unable to connect to smtp.office365.com HOT 2
- I need help regarding IMAP (Stopping, filtering messages etc.) HOT 14
- Is this the correct way to combine 4 SearchQuery in AND? HOT 1
- Receiving SSL Errors after connecting 100's ImapClients at the same time imap.gmail.com HOT 3
- MailKit fails on Unicode password HOT 4
- How can I receive a IMAP message with meta data and message (text and html), but without attachments HOT 2
- Does ImapFolder.MessageFlagsChanged report the UniqueId constantly? HOT 1
- Questions to Pop3Client.DeleteMessage HOT 1
- How to transfer an "inline" image via SmtpClient.Send HOT 4
- Fetching mails from outlook.com with IMAP fails HOT 3
- Possible to retrieve message that includes flags, from address, subject, and body content HOT 5
- ImapClient.ConnectAsync fails in v4.2.0: Syntax error in CAPABILITIES. Unexpected token: '+' HOT 10
- Syntax error in CAPABILITIES. Unexpected token: '+' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mailkit.