z
 
:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  
 

Resources

Finding Correct Content Managemet System
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Workflow Managemet Systems
Workflow management is a crucial component in organizing a variety of business processes so that they benefit the business as a whole and increase profitability...

Using the Power of Content Management Systems
With page editors that resemble a word processor program, adding content with a CMS interface is simple and fun. Most CMS software also allows you to change the location of your content pages and links easily, while the back end processes takes care of updating the links throughout your site...

Content Management Systems (CMS): What They Are And Why We Love Them
In the past, individuals who took interest in having and operating their own websites were burdened with the task of learning HTML, DHTML, and other web-based technologies such as JavaScript and CSS. The only alternative to this was, unfortunately, to pocket the expenses and costs required to pay a web developer to build and maintain it for them...

Outsourcing
Post your project for outsourcing and get bids from qualified programmers, designers, interpreters, copywriters.


 

Code Walkthroughs

Datagrid Formatting the Data
We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. In our example we shall format the column to have to digits after the decimal point , followed by a...

Datagrid Highlight a Row With Click Through
It is relatively easy to add alternating colours to the rows in your datagrid. However, when we move the mouse over the rows we may want to highlight this row, and possibly to add the option of a click through based on the row selected...

Add a Delete Button to a Datagrid
To add a delete button to a datagrid follows a similar process to adding an edit button. In the datagrid header...

Add an Edit Button to a Datagrid
The datagrid has a predefined editColumn for handling the editing of a datagrid. Adding this simple column definition to a datagrid adds a powerful feature. When a row is not in edit mode the column item shows the word...

Making a Datagrid Row Editable
Two of the most popular methods of editing a datagrid in asp.net are to either select the row and take the user off to a different presentation of the data, or to change the formatting of the row presented in the database with appropriate edit text boxes, checkboxes and...

Adding Tooltips to Datagrid Rows
Adding tooltips to datagrid rows is easy, assuming that you have already created the code for adding row highlighting. In this article I shall assume that you have already read the article entitled Datagrid Highlight a Row With Click Through...

Binding a Datagrid to an Access Database
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Adding Data to a DropDownList
The aim of this article is to answer the question 'How do I add items to a DropDownList?' Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP...

Getting Current Date Time
In classic ASP we had now() which would return the current date and time. For asp.net this no longer exists. So what should we use...

Test if File Exists
Sometimes, in order to reduce our chance of error, when working with the filesystem in ASP.NET, we need to determine wether a file exists before performing an action on it. The following short piece of code will enable us to test whether a file exists...

Using Javascript with ASP.NET Form Elements
Adding simple pieces of Javascript to an Asp.net page can be acheived by adding to the attributes of the particular imagebutton or linkbutton. if its normal ASP.Net Button then you can...

Regular Expressions
In the table below we list the characters used in .Net regular expressions, together with their meaning, But first...

Authentication in Asp.net
Forms authentication in ASP.Net is far more easier and safe than Asp 3. It is possible to place a web.config file in any directory of a web site.Therefore, we are able to make most of a web site public, whilst providing authentication on, say, one directory...

Discussion Forums

General ASP.NET

.Net Programming

cSharp Home

Sql Server Home

Javascript / Client Side Development

IT Jobs

Ajax Programming

Ruby on Rails Development

Perl Programming

C Programming Language

C++ Programming

Python Programming Language

Laptop Suggestions?

TCL Scripting

Fortran Programming

Scheme Programming

18. Mail

FAQ Home
   18.1 How to send HTML mail in ASP.NET?
   18.2 How to send simple text mail in ASP.NET?
   18.3 What is the namespace used for sending mails?
   18.4 How do I configure a local SMTP server?
   18.5 Why do I get the error message "The transport failed to connect to the server "?
   18.6 Why do I get the error message "The SendUsing configuration value is invalid"?
   18.7 How can I read POP3 email with ASP.NET?
   18.8 How do I email a website exception?
   18.9 How do I send a web page?
   18.10 How do I send non US-ASCII mails?
   18.11 How do I send an email with attachments?
   18.12 How do I add the Reply-To header to the MailMessage?
   18.13 Why do I get the error message "Sender address rejected: need fully-qualified address "?
   18.14 How do I resolve the error "The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication. Please check your mail client settings"?
   18.15 How can I specify multiple recipients for a message?
   18.16 How can I use a "friendly name" in the To and From properties?
   18.17 Why do I get the error message "550 5.7.1 Unable to relay for xxx or 550 not local host xxx, not a gateway "?

18.1 How to send HTML mail in ASP.NET?


VB.NET


Dim mail As New MailMessage()
mail.To = "manag@megasolutions.net"
mail.From = "admin@megasolutions.net"
mail.Subject = "Test."
mail.BodyFormat = MailFormat.Html
mail.Body = "Test Message"
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)


C#


MailMessage mail = new MailMessage();
mail.To = "manag@megasolutions.net";
mail.From = "admin@megasolutions.net";
mail.Subject = "Test.";
mail.BodyFormat = MailFormat.Html;
mail.Body = "Test Message";
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );



18.2 How to send simple text mail in ASP.NET?


VB.NET


Dim mail As New MailMessage()
mail.To = "admin@megasolutions.net"
mail.From = "manag@megasolutions.net"
mail.Subject = "Test."
mail.Body = "Test Message"
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)


C#


MailMessage mail = new MailMessage();
mail.To = "admin@megasolutions.net";
mail.From = "manag@megasolutions.net";
mail.Subject = "Test.";
mail.Body = "Test Message";
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );



18.3 What is the namespace used for sending mails?


Use System.Web.Mail
The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. The classes in this namespace can be used from ASP.NET or from any managed application.
For more details refer System.Web.Mail Namespace


18.4 How do I configure a local SMTP server?


Install the SMTP virtual server that is part of IIS.

  • You will need your Windows installation CD.
  • Use Add or Remove Programs in the Windows Control Panel to launch Add/Remove Windows Components.
  • Select Internet Information Services (IIS) and then click Details.
  • Check SMTP Service and then click OK.
  • The installation process will prompt you for your Windows CD and will install the SMTP virtual server.

After installing the SMTP server (also reboot), configure it by following these steps:
  1. In the Control Panel, choose Administrative Tools, and then choose Internet Information Services.
  2. Open the node for your computer, right-click the Default SMTP Virtual Server node and choose Properties.
  3. In the Access tab, click Connection.
  4. Select Only the list below, click Add and add the IP 127.0.0.1. This restricts connections to just the local computer, i.e., localhost. Close the Connection dialog box.
  5. Click Relay and repeat Step 5 to allow only localhost to relay through this server.
    In your firewall or router (or both), close incoming port 25. This is an important security measure that will prevent spammers from finding your SMTP server and using it to relay spam.

 


18.5 Why do I get the error message "The transport failed to connect to the server "?


This is a network related error. Your application cannot connect to the mail server specified.
Make sure that the following are true about SmtpMail.SmtpServer:

  • Is a valid SMTP Server
  • Make sure that the server System.Web.Mail is running or can connect to the mail server. Some times firewalls or proxy servers can get in the way.
  • Try specifying the value by IP address. Poor DNS resolution can sometimes hinder name lookups.
  • Make sure that the mail server is running at port 25.
  • If you did not specify a SmtpMail.SmtpServer property, or if SmtpMail.SmtpServer points to "localhost" (or the equivalent), be sure the SMTP Service is running on port 25.
  • For testing purposes change the MailMessage.From and MailMessage.To properties to an address that exists on SmtpMail.SmtpServer. Some times this exception is thrown, when it really is a relay issue.

 


18.6 Why do I get the error message "The SendUsing configuration value is invalid"?


This problem occurs because read access to the Internet Information Services (IIS) metabase and the Microsoft Active Directory directory service has been removed for the Everyone group. This access has been removed because of a security modification in Exchange 2000 Server SP3. CDOEX, CDOSYS, and System.Web.Mail must have access to the IIS metabase to access information about the location of the pickup directory path. This behavior occurs when you use the Sendusingpickup method and if this information is not specified in the application code. Because access is restricted, the non-administrative user under whose security context the application is running cannot read this information from the IIS metabase and Active Directory. Fore more information refer Read Access to the Everyone Group Is Removed After You Install Exchange 2000 SP3


18.7 How can I read POP3 email with ASP.NET?


Refer Read POP3 Email with ASP.NET


18.8 How do I email a website exception?


VB.NET


Protected Sub Application_Error(sender As [Object], e As EventArgs)
     Dim ex As Exception = Server.GetLastError()
     Dim mail As New MailMessage()
     mail.To = "manag@megasolutions.net"
     mail.From = "admin@megasolutions.net"
     mail.Subject = ex.Message.ToString()
     mail.Body = ex.ToString()
     mail.BodyFormat = MailFormat.Html
     SmtpMail.SmtpServer = "localhost"
     SmtpMail.Send(mail)
End Sub 'Application_Error


C#


protected void Application_Error(Object sender, EventArgs e)
{
     Exception ex = Server.GetLastError ();
     MailMessage mail = new MailMessage();
     mail.To = "manag@megasolutions.net";
     mail.From = "admin@megasolutions.net";
     mail.Subject = ex.Message.ToString ();
     mail.Body =ex.ToString ();
     mail.BodyFormat = MailFormat.Html;
     SmtpMail.SmtpServer = "localhost";
     SmtpMail.Send( mail );
}



18.9 How do I send a web page?


Use namespace System.IO and System.NET VB.NET


'In Page_Load
Dim mailcontenturl As String = "http://www.megasolutions.net"
Dim mail As New MailMessage()
mail.To = "manag@megasolutions.net"
mail.From = "admin@megasolutions.net"
mail.Subject = "Test."

'To send webpage
mail.Body = GetHTML(mailcontenturl)
mail.BodyFormat = MailFormat.Html
mail.UrlContentBase = mailcontenturl

SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(mail)


'Function GetHTML
Private Function GetHTML(url As String) As String
     Dim wReq As WebRequest = System.Net.HttpWebRequest.Create(url)
     Dim sr As New StreamReader(wReq.GetResponse().GetResponseStream())
     Dim result As String = sr.ReadToEnd()
     sr.Close()
     Return result
End Function 'GetHTML


C#


//In Page_Load
string mailcontenturl ="http://www.megasolutions.net";
MailMessage mail = new MailMessage();
mail.To = "manag@megasolutions.net";
mail.From = "admin@megasolutions.net";
mail.Subject = "Test.";

//To send webpage
mail.Body = GetHTML ( mailcontenturl );
mail.BodyFormat = MailFormat.Html;
mail.UrlContentBase = mailcontenturl;
          
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send( mail );

//Function GetHTML
private string GetHTML( string url )
{
     WebRequest wReq= System.Net.HttpWebRequest.Create(url);
     StreamReader sr = new StreamReader( wReq.GetResponse().GetResponseStream() );
     string result = sr.ReadToEnd();
     sr.Close();
     return result;
}



18.10 How do I send non US-ASCII mails?


VB.NET


mail.BodyEncoding = System.Text.Encoding.GetEncoding( "ISO-2022-JP" )


C#


mail.BodyEncoding = System.Text.Encoding.GetEncoding( "ISO-2022-JP" );



18.11 How do I send an email with attachments?


VB.NET


Dim mailattach As New MailAttachment(Server.MapPath("<yourfilename>"))
mail.Attachments.Add(mailattach)


C#


MailAttachment mailattach = new MailAttachment( Server.MapPath( "<yourfilename>" ) );
mail.Attachments.Add( mailattach );     



18.12 How do I add the Reply-To header to the MailMessage?


VB.NET


mail.Headers.Add( "Reply-To", "admin1@megasolutions.net" )


C#


mail.Headers.Add( "Reply-To", "admin1@megasolutions.net" );



18.13 Why do I get the error message "Sender address rejected: need fully-qualified address "?


This is happening because your SmtpMail.SmtpServer is rejecting addresses.
To resolve this make sure

  • all email addresses specified at MailMessage.To, MailMessage.Cc, MailMessage.Bcc and MailMessage.From are valid email addresses
  • you have permissions to relay through the server.
  • MailMessage.From has permissions to relay through the server

 


18.14 How do I resolve the error "The server rejected one or more recipient addresses. The server response was: 503 This mail server requires authentication. Please check your mail client settings"?


CDOSYS supports username and password authentication, and version 1.1 of the .NET Framework added the Fields property to the MailMessage class, which lets you set arbitrary fields of the underlying CDOSYS object.
Refer Extending System.Web.Mail


18.15 How can I specify multiple recipients for a message?


Seperate each recipient with a semicolon (;) in the mailMessage.To property:
VB.NET


mailMessage.To = "abc@megasolutions.net;xyz@megasolutions.net"


C#


mailMessage.To = "abc@megasolutions.net;xyz@megasolutions.net";