Autore Topic: Sul componente SMTP  (Letto 555 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.725
  • Ne mors quidem nos iunget
    • Mostra profilo
Sul componente SMTP
« il: 22 Ottobre 2014, 17:13:31 »
Riporto questa discussione:


" I've a problem with gambas 3.6 Smtpclient (with previous versions too).
perhaps I don't understand very well the SMTP protocol or I don't undestand
what some propertis of SmtpClient are, but If someone can help me I'll
appreciate it very much:

I use this function to send eMails:

Codice: gambas [Seleziona]
Public Sub enviarmail(aTo As String[], cSubject As String, cTexto As
String, cFrom As String, Optional aAttacheds As String[], Optional bcc As
String[])

    Dim SmtpC As New SmtpClient
    Dim s, fich, mime, fname As String

    SmtpC.debug = True
    SmtpC.host = mcomun.mailserver["host"]
    SmtpC.user = mcomun.mailserver["user"]
    SmtpC.password = mcomun.mailserver["password"]
    SmtpC.From = cFrom
    For Each s In aTo
        SmtpC.To.Add(s)
    Next
    SmtpC.Subject = cSubject
    SmtpC.Body = cTexto
    SmtpC.Alternative = False    ''Not sure about this is for
    If Not IsNull(aAttacheds) Then
        For Each fich In aAttacheds
            Exec ["file", "-bi", fich] To mime
            mime = Left(mime, InStr(mime, ";") - 1)
            fname = Right(fich, - RInStr(fich, "/"))
            SmtpC.Add(File.Load(fich), mime, fname)
        Next
    Endif
    If Not IsNull(bcc) Then
        For Each s In bcc
            SmtpC.bcc.Add(s)
        Next
    Endif
    SmtpC.Encrypt = Net.SSL
    SmtpC.send

End

All works fine, but when a text file is attached the content of file is
showed under the .Body content. Sending the same file with Thunderbird, The
content of the message is both the same, except this, relative to
attachement:

Content-Type: text/plain; charset=UTF-8;
name="ftp.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="ftp.txt"

The message that send the smtp client has:

Content-Type: text/plain; name="sirigamin.txt"
Content-Transfer-Encoding: quoted-printable
Content-Length: 13

abcdefghijklmn

Seems that Gambas smtpclient doesn't base64 encode the file attached if is
plain/text...
Is this a expected behavior?  Can it be changed or the encode forced?

Thanks in advance
Jorge
"


" I have added a source archive with application MailTester

1. It allows you to test mail with the settings entered in the form
fields. Note that these are saved as setings (using gb.settings) and the
password is stored in keyring (using gb.desktop) except for attachment
path.

2. It also allows you to browse for an attachment and add it to the test
mail.

3. It sends the mail to your from mail address and adds some body
content to let you know it worked.

4. If using authentication it needs Gambas 3.6 at least (as
authentication was still buggy pre 3.6)

You can test MailTester and if it works for you compare the code to
yours.

I have tested this with .png, .tar, .txt and .deb attachments and they
are all nicely added.

I also attached a screenshot of one of the test mails received.


Hope this will help.

--
Kind regards,

Willy
"


" Willy, thank you for your response.

With Mailtester the same situation occurs that with my function: If you
attach a text file, it is enconded as "cuoted printable" (what is the same
that "no enconded as all") and the text from inside the file is shown under
the message .Body...

Jorge
"


" I have taken a closer look and I do notice one difference between 3.5.4
and 3.6

With gb.smpt 3.5.4 I get the mail in my inbox and see a paperclip icon
indicating an attachment.

With gb.smtp 3.6 there is no longer a paperclip icon in my inbox. But
the content appears the same, and the attachment is there in the same
way as with 3.5.4 (so really attached and not part of the content as
happened with some pre 3.5.4 version, don't remember the version)

So apparently, something is different under the hood..

--
Kind regards,

Willy
"


" Jorge,

I took your code and modified it only enough to use a form to populate
the relevant data; all else remained the same.

Testing it showed that the attachment was indeed sent as
"Content-Transfer-Encoding: quoted-printable".

But, that does not appear to be a problem; at least with my Thunderbird.
The attachment was sent as a true attachment (showing the paperclip) and
not included in the message body.

Perhaps Base64 encoding is not required for plain text and, hence, the
reason Gambas does not bother with it.

--
Lee
"


" The encoding format is not related at all with how the mail client
displays a mail part: as attachment or inside the mail.

Curiously, there is no concept of "mail body" in the MIME message format.

Mail clients usually take the first part that is text or HTML as the
body. But I think you can help him by using the "Content-Disposition"
header.

I will make some tests as soon as I have time.

Regards,

--
Benoît Minisini
"


" Lee,
Yes, the my function works, it's only the related way of show in email
readers what doesn't like me. Sometimes we have to send large text files
and it's ugly to see that in thunderbird...
Is nota a big issue, I suppouse. "Perfection is reserved for gods", isn't
it?.

Thanks for the response.



Benoit,
Thanks for the answer. I

>Curiously, there is no concept of "mail body" in the MIME message format.
I knew that, but SmtpClient offers it and its easy way to send a message.
At this point there is another property that isn't clear for me: the
.Alternative property.
I've read, don't remember where, that is a information passed to old email
clients who hasn't reading .html capacities. If you set it a True and send
a text content as first part of MIME, the second part is sopused to be the
HTML version of the first part. However I do not see any difference between
use it as True or False. I mention it because, if it is so, when the
.Alternative is True the message should be 1.- text of message 2.- HTML
version of same text 3.- attached files, and if .Alternative is False then
should be: 1.- text 2.- attached file.
Or is .Alternative something obsolete or I don't understand nothing and all
this is a mess.... :D:D:D

>But I think you can help him by using the "Content-Disposition" header.
How can it be implemented? I haven't seen any way in SmtpClient.

Regards

Jorge
"


" In revision #6574, I added a "Content-Disposition" header to each
attachment to tell him if it should be "inlined" (when you don't give an
explicit name to the attachment with the Add method) or "attached" (when
you give an explicit name).

But apparently it has no effect in my Thunderbird.

Can you tell me if it has an effect for you?

--
Benoît Minisini
"
« Ultima modifica: 25 Ottobre 2014, 21:31:14 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »