E-mail attachment with unwanted characters

Vino.B vino.bheeman at hotmail.com
Fri Apr 27 17:57:26 UTC 2018


Hi All,

   Request your help, the below code is working as expected, but 
when I receive the attachment, the attachment contains the 
orginal text plus some unwanted characters like below, can 
someone help me how to remove these unwanted characters.

Unwanted characters
This is a test documentoÞ
óÎ}ã¿xÛ]Zõ§ûwN¶÷ÆÝy·

Code:
import std.base64: Base64;
import std.container.array;
import std.conv : to;
import std.file: read;
import std.format : format;
import std.net.curl;
import std.path : baseName;
import std.uuid: randomUUID;
pragma(lib, "curl");

string Message(string To, string From, string Body, string 
Subject, string Filename, ubyte[] Content) {
Array!string headers;
string cid, msg, boundary = randomUUID().toString(), Fname = 
baseName(Filename);
const string crlf = "\r\n";
int Size = to!int(getSize(Filename));

     headers.insert("From: " ~ From );
     headers.insert("To: " ~ To );
     headers.insert("Subject: " ~ "Subject" );
     headers.insert("MIME-Version: 1.0");
     headers.insert(format("Content-Type: multipart/mixed; 
boundary=\"%s\"\r\n", boundary));
     headers.insert("--" ~ boundary);
     headers.insert("Content-Type: text/html; charset=utf-8");
     headers.insert(crlf);
     headers.insert(Body);
     headers.insert(crlf);
     headers.insert("--" ~ boundary);
     headers.insert("Content-Type: text/plain");
     headers ~ ((cid !is null) ? "Content-ID: <" ~ cid ~ ">" : "");
     headers.insert("Content-Transfer-Encoding: base64");
     headers.insert("Content-Disposition: attachment; filename=\"" 
~ Fname ~ "\"");
     headers.insert(crlf);
     headers.insert(to!string(Base64.encode(Content)) ~ ".\r\n");
     headers.insert(crlf);
     headers.insert("--" ~ boundary ~ ".");

     msg.reserve(Size + Body.length);
     foreach(header; headers) { msg ~= header ~ "\r\n"; }
     if(msg.length > 0) { msg ~= "\r\n";}
     return(msg);

  }

  void main () {
  auto Filename = "C:\\Script\\New\\new.txt";
  auto Con = cast(ubyte[])read(Filename);
  auto smtp = SMTP("smtp://xxx.com");
  smtp.mailTo = "user at ask.com";
  smtp.mailFrom = "admin at ask.com";
  smtp.message =  Message("user at ask.com", "admin at ask.com", "Test", 
"TestMail", Filename, Con);
  smtp.perform();
}

From,
Vino.B



More information about the Digitalmars-d-learn mailing list