Comments (13)
In general, the top-level Multipart of the message is a multipart/mixed which contains all of the attachments. So just add them all to that part.
If it's not a multipart/mixed, create a new multipart/mixed, add the existing message.Body to that, then add all the remaining attachments.
from mailkit.
This is why I keep urging you to study up on MIME and the common MIME structures.
from mailkit.
I would recommend either using the MimeIterator or MimeVisitor to traverse the MIME structure of the message so that you always know what the parent MIME (multipart and/or message-part) part is which would allow you to replace it.
Personally, for your task, I'd recommend MimeIterator as being the simplest.
using (var iter = new MimeIterator (message)) {
while (iter.MoveNext ()) {
if (iter.Parent is Multipart multipart && iter.Current is MimePart part && part.IsAttachment) {
int index = multipart.IndexOf(part);
multipart[index] = replacementPart;
}
}
}
from mailkit.
How can I create a new MimeEntity from a file to put as the replacementPart ?
@jstedfast
from mailkit.
This is the code I have come up with so far, i cannot quite understand how to remove the attachments from Attachments object because it is readonly...
var newAttachments = Directory.EnumerateFiles(attachsFolder);
foreach (var attachment in newAttachments)
{
using (var iter = new MimeIterator(message))
{
while (iter.MoveNext())
{
if (iter.Parent is Multipart multipart && iter.Current is MimePart part && part.IsAttachment)
{
multipart[multipart.IndexOf(part)] = new MimePart()
{
Content = new MimeContent(File.OpenRead(attachment)),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName(attachment),
};
}
}
}
}
foreach (var attachment in message.Attachments)
{
newMessage.Attachments.Append(attachment);
}
I don't know if this is the right approach, i will be testing for correctness anyway.
New Edit: the if condition is never hit, even though first iter
seems to have one of it's current's as the object which contains the file that i want to replace...
from mailkit.
This is the code I have come up with so far, i cannot quite understand how to remove the attachments from Attachments object because it is readonly...
You can't. The Attachments
property walks the entire MIME tree of the message and yield return
's the MimeParts that are attachments. There is no actual List<MimePart>
data structure anywhere that references the message's Attachments. That's why you can't set, Add() or Remove() anything from the MimeMessage.Attachments
property. Pretend that property doesn't exist for what you are trying to do because it will not help you.
Next, I recommend reading up on the MIME specifications that you can find here: https://github.com/jstedfast/MimeKit/blob/master/RFCs.md - specifically, read RFC2045. The rest are about encodings which aren't important (MimeKit handles all that for you). While MimeKit handles the parsing of messages according to the rules in RFC2045, you NEED to understand the MIME structure so that you can do what you are trying to accomplish.
MIME is NOT a list of attachments and a body. It is a tree structure and you need to understand how it is structured for your task.
Then you need to make sure to duplicate that structure in the newMessage
that you are creating.
The next thing you'll need to remember is that when the message
goes out of scope, it will eventually get disposed (unless it gets Dispose()'d directly by you), at which point, all of the child MimeParts will also get disposed. You'll need to make sure to account for that if you are going to re-use any parts from message
in newMessage
.
from mailkit.
I have some knowledge about the topic in terms of networking and data transfering...
But right now I'm just trying to solve the issue, and yes. I know it is not a simple data structure but it is just a simple task and go, not some big project that i'm making a full-featured solution for it...
So as of programming, I don't know what exactly to do with the Current or Parent, as explained i could find the attachment that i need in the Current, but don't know how to access this one and edit it with the code provided ....
Thanks for clarifying!
Edit: to explain more, I mean using VS 2022 debugger i could inspect the object and find what i need.
from mailkit.
The next loop iteration will descend into its children.
In other words: iter.MoveNext()
will move to the child.
Your if-statement looks wrong: if (iter.Current is Multipart multipart && iter.Current is MimePart part && part.IsAttachment)
See anything wrong with that statement? :-)
You need if (iter.Parent is Multipart multipart && iter.Current is MimePart part && part.IsAttachment)
from mailkit.
Thank you, although it looks identical to your last response but this one actually went through the condition and parsed the needed content.
Then my last code worked and did the job and now new files with new names and new content appear in the new message ;)
from mailkit.
Do you think this is the best approach to put all files in the directory into attachments? or the code needs to be changed.?
from mailkit.
The code needs to be changed because you have have more files in the directory than the original message has attachments.
from mailkit.
How can I achieve it so that whatever files I have in the folder, get loaded as an attachment?
from mailkit.
Thank you for the effort, I know I have been asking alot of questions, but this will help in future incase someone wants something related to the work i'm trying to do 😂👀
from mailkit.
Related Issues (20)
- MimeMessage.TextBody not returning body HOT 3
- Code style: use <inheritdoc cref="..."> instead of copying doc HOT 5
- I get a couple of compile errors when updating MailKitLite from 4.3.0 to 4.4.0 HOT 2
- MimeMessage.Subject == null throws off the MailVisitor.
- Regression: Nested signed mail showed as nameless.eml HOT 3
- Question: Logging EHLO Response in DB HOT 2
- XLIST IMAP command being sent automatically on AuthenticateAsync HOT 3
- UTF-8 not supported ?! HOT 1
- client_credentials flow fails for SMTP Office 365 HOT 1
- Special Character on Password HOT 3
- MailKit Login Issue: Stalling on Inbox Access HOT 2
- With version 4.4.0 I cannot send SMTP mail via Strato.de HOT 11
- Cannot delete message from IMPAP account. HOT 4
- Accessing the IP address of devices logged into the e-mail account HOT 1
- Recieved MimeMessage contains wrong embedded Images HOT 1
- IMAP client connected and authenticated but Inbox returns null HOT 4
- in the sample project of imapidle, is it possible to add a sample method for how to cancel the "IDLE" mode. thanks! HOT 2
- SmtpProtocolException - Last Smtp Response before failure HOT 6
- SMTP not working with Strato.de HOT 4
- SmtpClient Exception Behavior Change in 4.4.0 HOT 26
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.