Vous êtes sur la page 1sur 7

Mail:

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";
18.16 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@megasolutions.net>"


mail.To = "Name <name@megasolutions.net>"

C#

mail.From = "Name <name@megasolutions.net>";


mail.To = "Name <name@megasolutions.net>";

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

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.