Jennie
Jennie

Reputation: 45

asp.net send mail

i am writing a program to send mail using asp.net, it gives me an error as the "SendUsing" configuration value is invalid. Here is my code

protected void Button2_Click(object sender, EventArgs e)
{
    MailMessage msg = new MailMessage();
    msg.To = "[email protected]";
    msg.From = "[email protected]";
    msg.Subject = "hello";
    SmtpMail.Send(msg);
}

Upvotes: 1

Views: 297

Answers (3)

shindigo
shindigo

Reputation: 1295

One additional comment: I found that on Server 2008, your website needs to run in an application pool with identity other than ApplicationPoolIdentity (the default) for e-mail to be sent via localhost (i.e., no SmtpMail.SmtpServer set). All of the other identities work (NetworkService, LocalService, LocalSystem), just not ApplicationPoolIdentity.

If you specify SmtpMail.SmtpServer as something other than null or localhost, ApplicationPoolIdentity can be used.

Upvotes: 0

Shoban
Shoban

Reputation: 23016

Typically this exception has to deal with the following line of code (or I should say ABSENCE of the following line of code):

SmtpMail.SmtpServer = "mail.your-domain.com"

By default, if SmtpMail.SmtpServer is not set, System.Web.Mail is supposed to use localhost as the default property. However, for some reason this doesn't appear work.

You could have done a simple google search for this ;-)

Source

Upvotes: 0

Jan Jongboom
Jan Jongboom

Reputation: 27342

You need to add the SMTP server you want to use, something like

System.Web.Mail.SmtpMail.SmtpServer = "mail.provider.com";

Perhaps you might want to take a look at the System.Net.Mail, which is the new mail class in .Net 2, System.Web.Mail is obsolete.

Upvotes: 3

Related Questions