PDA

View Full Version : SMTP tag for attaching file



joseph
5th July 2007, 14:11
Hi,
While sending a mail, the recipient receives the mail with all the contents of the file as a text. I dont want the file contents to be shown directly. I want the file to sent as an attachment to mail. As per my analysis, the following are the standard tags read by SMTP. Now to attach a file there has to be some tag which will tell SMTP to attach it. Please help me to find the tag and where it should be placed in the example below.


HELO there
MAIL FROM : < xyz@gmail.com >
RCPT TO : < abc@yahoo.com >
DATA
From: xyz@gmail.com
To: abc@yahoo.com
Subject: Hi..!!!
Hello How are u...???
QUIT

Thanks in advance

joseph
5th July 2007, 14:59
Hi guys,
Got it... Please let me know if I'm wrong...
Added this string after DATA tag

QString header;
header += QString("Mime-version: 1.0") + QString("\r\n");
header += QString("Content-Type: text/plain; charset=ISO-8859-1") + QString("\r\n");
header += QString("Content-Transfer-Encoding: 7bit") + QString("\r\n");
header += QString("Content-Disposition: attachment;") + QString( "filename=" ) + "fileName" + QString("\r\n");


HELO there
MAIL FROM : < xyz@gmail.com >
RCPT TO : < abc@yahoo.com >
DATA
Mime-version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; fileName
From: xyz@gmail.com
To: abc@yahoo.com
Subject: Hi..!!!
Hello How are u...???
QUIT


Thanks a lot and please comment

wysota
5th July 2007, 15:04
I think you should send a multipart mime type, something like:

Content-Type: multipart/related; boundary="----------=_marker"; type="text/html"

< here goes the mail text and headers >

------------=_marker
Content-Type: text/html; charset="iso-8859-2"
Content-Disposition: attachment; fileName
MIME-Version: 1.0

attachment goes here
------------=_marker
Content-Type: text/html; charset="iso-8859-2"
Content-Disposition: attachment; fileName
MIME-Version: 1.0

another attachment goes here
------------=_marker

joseph
6th July 2007, 04:58
Hi wysota,
Thanks for your reply.
I have a confusion. What is the difference between first solution and your solution???

I tried your solution, the application says "MESSAGE SENT" but the mail is not received. I did this

QString Header;
Header += QString("Content-Type: multipart/related;") +
QString("boundary=--_marker;") +
QString("type=text/html\r\n");

QString msgContent;
msgContent += QString("--=_marker\r\n");
msgContent += QString("Content-Type: text/html; charset=ISO-8859-2") + QString("\r\n");
msgContent += QString("Content-Disposition: attachment;") + QString( "filename=" ) + "fileName" + QString("\r\n");
msgContent += QString("Mime-version: 1.0") + QString("\r\n");

QString attachmentContent;
attachmentContent += QString("--=_marker\r\n");
attachmentContent += QString("Content-Type: text/html; charset=ISO-8859-2") + QString("\r\n");
attachmentContent += QString("Content-Disposition: attachment;") + QString( "filename=" ) + "fileName" + QString("\r\n");
attachmentContent += QString("Mime-version: 1.0") + QString("\r\n");

*t << "DATA\r\n"; //here t is QTextStream
*t << Header;
*t << msgContent;
*t << attachmentContent;
These contents are sent to SMTP

HELO there
MAIL FROM: <xyz@gmail.com>
RCPT TO: <abc@yahoo.com>
DATA
Content-Type: multipart/related;boundary=--_marker;type=text/html
--=_marker
Content-Type: text/html; charset=ISO-8859-2
Content-Disposition: attachment;filename=fileName
Mime-version: 1.0
--=_marker
Content-Type: text/html; charset=ISO-8859-2
Content-Disposition: attachment;filename=fileName
Mime-version: 1.0
From: xyz@gmail.com
To: abc@yahoo.com
Subject: Hi..!!!
Hello How are u...???

QUIT

Any idea???

One more thing.. why is below code used in the smtp example

message.replace( QString::fromLatin1( "\n" ),
QString::fromLatin1( "\r\n" ) );
message.replace( QString::fromLatin1( "\r\n.\r\n" ),
QString::fromLatin1( "\r\n..\r\n" ) );
If I remove this code the output is still same...
Thanks

wysota
6th July 2007, 07:11
1. You didn't set any headers in the main part of the mail.
2. \r\n is the "network" format of a newline and you should stick with it. Most applications will work without it, but stil...

joseph
6th July 2007, 07:40
You didn't set any headers in the main part of the mail.
What header and where to set it?? Can you please add what header and where to set it in the below given example.


HELO there
MAIL FROM: <xyz@gmail.com>
RCPT TO: <abc@yahoo.com>
DATA
Content-Type: multipart/related;boundary=--_marker;type=text/html
--=_marker
Content-Type: text/html; charset=ISO-8859-2
Content-Disposition: attachment;filename=fileName
Mime-version: 1.0
--=_marker
Content-Type: text/html; charset=ISO-8859-2
Content-Disposition: attachment;filename=fileName
Mime-version: 1.0
From: xyz@gmail.com
To: abc@yahoo.com
Subject: Hi..!!!
Hello How are u...???

QUIT
Thanks

joseph
9th July 2007, 05:35
Hi wysota,
Here is what I have been trying...

//Message to SMTP
HELO there
MAIL FROM: <xyz@gmail.com>
RCPT TO: <abc@yahoo.com>
DATA
Content-Type: multipart/related; boundary=unique-boundary-1
--unique-boundary-1
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7Bit
Content-Disposition: attachment; filename=fileName
MIME-Version: 1.0
From: xyz@gmail.com
To: abc@yahoo.com
Subject: Hello
How is going?
.
QUIT

Now i"m able to receive the mail but still there is some problem. The "Subject:" and "To: " part in the mail is empty. There is no information in the "Subject:" and "To:" line.

Is there anything I'm doing wrong????
Please help.

joseph
9th July 2007, 10:42
Hi wysota,
Please help me to solve this.
There is another problem, I want to encrypt the file and send via mail. I'm able to send the file but unable to open the file again.

Please help, I'm totally bugged.

Thanks

wysota
9th July 2007, 15:27
There has to be an empty line between the last header and the beginning of the body.

About the encryption - Remember that you can't send characters with ascii numbers larger than 127 via mail. You have to encode them using base64 or uuencode.

Please read some SMTP specification documents, at least take a look at the RFCs.

joseph
9th July 2007, 16:33
About the encryption - Remember that you can't send characters with ascii numbers larger than 127 via mail. You have to encode them using base64 or uuencode.

Hi wysota,
Now the real problem starts. I have written an function to encrypt and decrypt the file. My target was to send an encrypted file via mail. Now since you tell that "ascii numbers larger than 127 via mail", I have to revert back again.
I have read about base64, and tried implementing the code. This is the code that I have made use of

QString Header;
Header += QString("MIME-Version: 1.0\r\n");
Header += QString("Content-Type: multipart/related; ") + QString("boundary=unique-boundary-1;") + QString( "type=\"text/plain\"\r\n") ;

QString headerContent;
headerContent += QString("--unique-boundary-1\r\n");
headerContent += QString("Content-Type: text/plain; charset=iso-8859-2") + QString("\r\n");
headerContent += QString("Content-Transfer-Encoding: base64\r\n");
headerContent += QString("Content-Disposition: attachment;") + QString( "filename=" ) + "fileName" + QString("\r\n");

Now here starts the problem. Once a mail has been sent, I'm getting some stupid result. Something with only 2 to 3 characters. Any idea why??? Infact the file is big....
Another doubt is once I'm able to encode the data, hoe do I decode it again.???

Please tell me at what part of code I need to modify .

Thanks in advance

wysota
9th July 2007, 23:33
As far as I remember base64 is implemented in Qt, so you can call a ready made function directly.

joseph
10th July 2007, 04:41
Hi wysota,
There is so much of confusion... I'm using SMTP example from Qtassistant to send mails. As per my thinking, I can specify Content-Transfer-Encoding: base64 to send an encoded data over network. But it doesn't work.

As per your words-->

As far as I remember base64 is implemented in Qt, so you can call a ready made function directly.
Makes me more confuse, as to where I should call the ready made function.??

Can you please be more clear about what you mean....Thanks

Is there a better way I can perform this..
My Requirement-->
I have an encrypted file encoded with AES::CBC mode with a password. I want to send this encrypted file via mail to others. How can I do that????

Please help..
Thanks