WPF FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

21. Mail

FAQ Home
   21.1 How do I transfer files to a remote server over the internet using .NET?
   21.2 How to send HTML mail in ASP.NET?
   21.3 What is the namespace used for sending mails?
   21.4 How do I configure a local SMTP server?
   21.5 How to send simple text mail in ASP.NET?
   21.6 Why do I get the error message "The transport failed to connect to the server "?
   21.7 Why do I get the error message "The SendUsing configuration value is invalid"?
   21.8 How can I read POP3 email with ASP.NET?
   21.9 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 "?
   21.10 How do I add the Reply-To header to the MailMessage?
   21.11 How do I send an email with attachments?
   21.12 How do I send a web page?
   21.13 How do I send non US-ASCII mails?
   21.14 How do I email a website exception?
   21.15 Why do I get the error message "Sender address rejected: need fully-qualified address "?
   21.16 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"?
   21.17 How can I specify multiple recipients for a message?
   21.18 How can I use a "friendly name" in the To and From properties?



21.1 How do I transfer files to a remote server over the internet using .NET?


Here is an article from ftp online: Transfer Files Over the Internet


21.2 How to send HTML mail in ASP.NET?


VB.NET


Dim mail As New MailMessage()
mail.To = "manag@syncfusion.com"
mail.From = "admin@syncfusion.com"
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@syncfusion.com";
mail.From = "admin@syncfusion.com";
mail.Subject = "Test.";
mail.BodyFormat = MailFormat.Html;
mail.Body = "Test Message";
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );



21.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


21.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.


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


VB.NET


Dim mail As New MailMessage()
mail.To = "admin@syncfusion.com"
mail.From = "manag@syncfusion.com"
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@syncfusion.com";
mail.From = "manag@syncfusion.com";
mail.Subject = "Test.";
mail.Body = "Test Message";
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );



21.6 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.


21.7 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. For more information refer Read Access to the Everyone Group Is Removed After You Install Exchange 2000 SP3


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


Refer Read POP3 Email with ASP.NET


21.9 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 "?


The SMTP server is not configured to allow you to relay. If you are working with a local SMTP server, you can configure it to allow relaying for IP 127.0.0.1.


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


VB.NET


mail.Headers.Add( "Reply-To", "admin1@syncfusion.com" )


C#


mail.Headers.Add( "Reply-To", "admin1@syncfusion.com" );



21.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 );     



21.12 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.syncfusion.com"
Dim mail As New MailMessage()
mail.To = "manag@syncfusion.com"
mail.From = "admin@syncfusion.com"
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.syncfusion.com";
MailMessage mail = new MailMessage();
mail.To = "manag@syncfusion.com";
mail.From = "admin@syncfusion.com";
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;
}



21.13 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" );



21.14 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@syncfusion.com"
     mail.From = "admin@syncfusion.com"
     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@syncfusion.com";
     mail.From = "admin@syncfusion.com";
     mail.Subject = ex.Message.ToString ();
     mail.Body =ex.ToString ();
     mail.BodyFormat = MailFormat.Html;
     SmtpMail.SmtpServer = "localhost";
     SmtpMail.Send( mail );
}



21.15 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


21.16 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


21.17 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@syncfusion.com;xyz@syncfusion.com"


C#


mailMessage.To = "abc@syncfusion.com;xyz@syncfusion.com";



21.18 How can I use a "friendly name" in the To and From properties?


Format the email address this way:
VB.NET


mail.From = "Name <name@syncfusion.com>"
mail.To = "Name <name@syncfusion.com>"


C#


mail.From = "Name <name@syncfusion.com>";
mail.To = "Name <name@syncfusion.com>";


© 2001-2008 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap