How to send an Email from ASP .NET


WEB.CONFIG SETTINGS

<appsettings>

<add key=”ToAddress” value=”myemail@test.com”>
<add key=”FromAddress” value=”error@test.com”>
<add key=”host” value=”HOST”>
</appsettings>
</span>


…….
<system.net>
<mailSettings>
<smtp deliveryMethod=”Network” from=”test@test.com”>
<network host=”localhost” port=”25″ defaultCredentials=”true”/>
</smtp>
</mailSettings>
</system.net>


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;

///
/// Summary description for MessageMailSend
///
public class MessageMailSend
{
public MessageMailSend()
{
//
// TODO: Add constructor logic here
//
}
public static bool SendMessage(string to,string from,string subject,string message)
{
try
{
MailMessage msg = new MailMessage();
msg.To = to;
msg.Subject = subject;
msg.From = from;
msg.BodyFormat = MailFormat.Text;
msg.Body = message;
SmtpMail.Send(msg);
return true;
}
catch (Exception exp)
{
}
return false;
}
}

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s