SMTP Mail

Vino vino.bheeman at hotmail.com
Thu Apr 12 15:58:33 UTC 2018


On Tuesday, 10 April 2018 at 15:18:19 UTC, Adam D. Ruppe wrote:
> On Tuesday, 10 April 2018 at 15:10:44 UTC, Vino wrote:
>> The variable "to" is of type string[] but we need it as 
>> Array!string
>> The variable "Subject" but we need it as Array!string.
>
> You'll have to convert them yourself if that is a must, but why 
> would you need that?

Hi Adam,

  I tried to replicate your code with what is required for me, and 
was able to successed 90% and stuck, can you please help me.

Using the below code are are able to receive the mail with the 
attachment, but the content of the attachment contains the Body 
text of the mail(Body1") rather than the original text and the 
mail body is empty, not sure where is the issue.

Code:
import std.net.curl;
pragma(lib, "curl");
import std.format;
import std.container.array;
import std.stdio;
import std.path;
import std.file;

struct RelayInfo { string server; }
struct MimeAttachment { string type; string filename; 
const(void)[] content; string id; }

class EmailMessage {

Array!string To, Body, headers;
string From, Subject, msg;

override string toString ()
{
	string T = "%-(%s,  %)".format(To[]); string B = "%-(%s,  
%)".format(Body[]);
	headers.insertBack("To: " ~ T);
	headers.insertBack("From: " ~ From);
	headers.insertBack("Subject: " ~ Subject);
	headers.insertBack("MIME-Version: 1.0");
     	msg.reserve(Body.length + 1024);
	foreach(header; headers) { msg ~= header ~ "\r\n"; }
	if(msg.length > 0) { msg ~= "\r\n"; msg ~= B; }
	
	return(msg);
}

const(MimeAttachment)[] attachments;

void addAttachment(string Fname, ) {
string mimeType = "text/plain";
string filename = baseName(Fname);
void[] content = read(Fname);
string id = null;

headers.insertBack("Content-Disposition: attachment; 
filename=\""~ filename ~"\"");
headers.insertBack("Content-ID: <" ~ id ~ ">");
headers.insertBack("Content-Type: text/plain; charset=UTF-8; 
file=" ~ filename);
attachments ~= MimeAttachment(mimeType, filename, content, id);
}

void send (RelayInfo mailServer = RelayInfo("smtp://localhost")) {
auto smtp = SMTP(mailServer.server);
string T = "%-(%s,  %)".format(To[]);
smtp.mailTo = T;
smtp.mailFrom = From;
smtp.message = toString;
smtp.perform();	
}
}

void main () {
string Filename = "D:\\DScript\\Tmailconfig.txt";
auto message = new EmailMessage();
message.To ~= "vino at xxx.com";
message.From = "Server at hosting.com";
message.Subject = "Test";
message.Body ~= "Body1";
message.addAttachment(Filename));
message.send(RelayInfo("smtp://smtp.awk.sed.com"));
}

From,
Vino.B


More information about the Digitalmars-d-learn mailing list