Wednesday, March 28, 2012

smtp mail body from txt file

I want to send smtp mail from ASP.NET 2 but I want the body of the email to be drawn from a saved .txt file. The format of the mail will be plain text.

Can anyone help with how to do this?

theres a method called "LoadFromFile()" and takes the path to the text file as a string

you can do somethign like

Message.Plain.Body.LoadFromFile("myfile.txt");

and that will do it. But double check in the help file to make sure thats the EXACT name of the method.

hth,
mcm


Here is some source, I copied it from MSDN, and edit it to drawn the body from "saved.txt"

namespace Microsoft.Samples.Mailer

{

// Mailer sends an e-mail.

// It will authenticate using Windows authentication if the server

// (i.e. Exchange) requests it.

staticclassMailer

{

enumMailMessagePart

{

From,

To,

Subject,

Message

}

staticvoid Main(string[] args)

{

if (args.Length < 4)

{

Console.WriteLine(

"Expected: mailer.exe [from] [to] [subject] [message]");

return;

}

// Set mailServerName to be the name of the mail server

// you wish to use to deliver this message

string mailServerName ="smtphost";

string from = args[(int)MailMessagePart.From];

string to = args[(int)MailMessagePart.To];

string subject = args[(int)MailMessagePart.Subject];

//This is the part I edited

string body = System.IO.File.ReadAllText ("saved.txt" ) ;

try

{

// MailMessage is used to represent the e-mail being sent

using (MailMessage message =

newMailMessage(from, to, subject, body))

{

// SmtpClient is used to send the e-mail

SmtpClient mailClient =newSmtpClient(mailServerName);

// UseDefaultCredentials tells the mail client to use the

// Windows credentials of the account (i.e. user account)

// being used to run the application

mailClient.UseDefaultCredentials =true;

// Send delivers the message to the mail server

mailClient.Send(message);

}

Console.WriteLine("Message sent.");

}

catch (FormatException ex)

{

Console.WriteLine(ex.Message);

}

catch (SmtpException ex)

{

Console.WriteLine(ex.Message);

}

}

}

}

Hope that helps


so whats the problem? does it give you an error or what? Shouldn't the name body be "Message" to follow what you enumed above it?

0 comments:

Post a Comment