Enough-Software / enough_mail

IMAP, POP3 and SMTP clients for Dart developers. Contains both low level as well as a high level API.
Mozilla Public License 2.0
104 stars 56 forks source link

MessageBuilder replace pre content when add new Text, HTML or File.... #193

Closed mminhlequang closed 2 years ago

mminhlequang commented 2 years ago

final builder = MessageBuilder.prepareMultipartAlternativeMessage() ..from = [MailAddress('My name', 'minh1@xxx.com')] ..to = [MailAddress('Your name', 'minh2@xxx.com')] ..subject = 'My first message' ..addTextPlain('hello world.') ..addTextHtml('

hello world

'); final file = File(xfile.path); await builder.addFile(file, MediaSubtype.imageJpeg.mediaType); await builder.addTextPlain('hello world2'); final mimeMessage = builder.buildMimeMessage(); final sendResponse = await client.sendMessage(mimeMessage);

  With code above, in minh2@xxx.com just see: hello world2
  don't have pre text, html and image file
robert-virkus commented 2 years ago

I noticed that you add the text/plain part twice. If you want to update that part, you can use code like this to update it:

final partBuilder = builder.getTextPlainPart();
if (partBuilder != null) {
  partBuilder.text = text;
} else {
  builder.addTextPlain(text);
}

Does this solve you problem?

mminhlequang commented 2 years ago

So, Do you know how we can add multi images and text?

mminhlequang commented 2 years ago

I noticed that you add the text/plain part twice. If you want to update that part, you can use code like this to update it:

final partBuilder = builder.getTextPlainPart();
if (partBuilder != null) {
  partBuilder.text = text;
} else {
  builder.addTextPlain(text);
}

Does this solve you problem?

Thanks but it can't solve my problem

robert-virkus commented 2 years ago

Ah, you are also setting a multipart/alternative as the root mime part. I realize that the example I give was wrong, you probably want something like this:

final builder = MessageBuilder()
  ..from = [MailAddress('Personal Name', 'sender@domain.com')]
  ..to = [
    MailAddress('Recipient Personal Name', 'recipient@domain.com'),
    MailAddress('Other Recipient', 'other@domain.com')
  ];
builder.addMultipartAlternative()
  ..addTextPlain('Hello world!')
  ..addTextHtml('<p>Hello world!</p>');
builder.addBinary(Uint8List.fromList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
    MediaSubtype.imageJpeg.mediaType,
    filename: 'helloworld.jpg',
);

final message = builder.buildMimeMessage();

The above code will create a multipart/mixed message with the multipart/alternative inside.

I will update the example code and I consider simplifying this use case for the next release.

mminhlequang commented 2 years ago

Wow, Thanks I will try You need write it in example 👯

robert-virkus commented 2 years ago

published correction now in 2.1.0

robert-virkus commented 2 years ago

Please close when it works for you

mminhlequang commented 2 years ago

Hi I don't work, you can try it at your side?

mminhlequang commented 2 years ago

final builder = MessageBuilder() ..from = [MailAddress('Personal Name', 'sender@domain.com')] ..to = [ MailAddress('Recipient Personal Name', 'minh@xxx.com'), // MailAddress('Other Recipient', 'other@domain.com') ] ..addMultipartAlternative( plainText: 'Hello world plain text!', htmlText: '

Hello world html text!

', ); final file = File(xfile.path); await builder.addFile(file, MediaSubtype.imageJpeg.mediaType);

  final sendResponse = await client.sendMessage(builder.buildMimeMessage());

  With new version, i updated same your example, and now it can sent html + image file, not have text: "Hello world  plain text". it's normally?
robert-virkus commented 2 years ago

Hi, yes that's expected. multipart/alternative means that you have two version of the same thing and each viewer displays the content part that it can display best. E.g. a command line mailer would show the plain text part while most other mail apps would choose to display the html part.

mminhlequang commented 2 years ago

Yes okay, thank you!