Introduction

System.Web.Mail can be used to send emails from .NET 1.1 applications. Sending simple emails is very easy. It is more complicated when you try to send emails using a SMTP server that requires authentication, or even when you just need to format the From name of the email you want to send.

Background

Here is the most simple piece of code that will send an email using C#:

Even more complicated will be to send an email using a SMTP server that requires authentication. For that, the Fields collection of the MailMessage object needs to be used. Here is the sample piece of code that will help you solve your problems:

There are SMTP servers that require SSL. One very well known example is Gmail. In order to send emails using the Gmail SMTP server, you need to specify that and also set the correct port. The following example can be used to send emails using the Gmail SMTP:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

Comments and Discussions

Hello,
The example works fine while I use the Asp.Net Development Server, but when I deploy the site on IIS the Send() method throw an exception: "The transport failed to connect to the server." Does anyone have a suggestion please!!
Thanks.

Your article is still very useful, there are still those of us maintaining .Net 1.1 systems! Thanks a lot.

I had to research a couple of errors while testing and thought I would share. Hints:
-Error "Could not access CDO.Message object" usually is useless, but LOOK AT THE INNER EXCEPTIONS, ALL OF THEM. That will help more.
-This inner exception error "The message could not be sent to the SMTP server. The transport error code was 0x80040217" means your user name or password is incorrect and the message was therefore rejected by the mail server.

I have been searching the net but couldnt find a solution to my
problem of sending emails to external domains in Asp.net 1.1.

I am not getting any error and the Mail Send message returns 1 or
success but the mail doesnt go. I am using the Mail Server as the
Companies Exchange Server which has full rights to send emails to any
domain. This is proved by the fact that if I sent email through
outlook to hotmail,gmail etc , it works fine. Also I have created a
user in the Exchange Server with full rights but still the problem
persists. I have copied my code below to give you an idea.

THANK YOU SO MUCH ABOUT YOUR TOPIC
AND IM INTEREST IN THIS
THE MAIL SEND ALRIGHT WIHT GMAIL SERVER
BUT WHEN I ATTACHMENT A FILE ( ADDING 2 LINED OF CODE)
I DONT KNOW WHEN MY MAIL HAS BEEN SENT OR NEVER?
THANK YOU.

This is one of the good samples to send email. But when i tried the sample with my gmail account as the sender and rediff mail account as the receiver, it is appearing in the bulk mails folder of rediff. may i know what is wrong?

One more thing, do you have any samples which exactly do the same thing but using MFC. I want to send a mail to gmail from my application.

COMException (0x80040213): The transport couldn't have connected to the server.
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
[HttpException (0x80004005): Could not access 'CDO.Message' object.]

i see this exception
{Exception Details: System.Runtime.InteropServices.COMException: The server rejected the sender address. The server response was: 530 5.7.0 Must issue a STARTTLS command first e14sm93229qba
}
plz send me an example code for sending from "gmail"domain...
and what other domains that has a free POP service..?
thanks

Just what I was looking for. I am trying (and failing) to send e-mails automatically to a GoDaddy secure server using the 2.0 .Mail classes, which is a relay server, but your code has given me new hope

And, by the way, may I compliment you on your graceful handling of feedback.

best, Bill Woodruff

"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis

This code helped me setup the authentication i needed to send e-mail internally with an intranet application. I was having a problem where exchange would sometimes accept and sometimes deny the e-mails sent from the application with no notification. Also, a lot of them liked to end up in the Junk Email folder.

Hi,
This is the exact soulution that I am looking for my application. But I developing my program in VB .NET (2005). Is there anyway to include EnhancedMailMessage.cs file in my VB .NET project and use directly? OR I need to convert the code to VB .NET equivalent?

I am glad you find this article helpful. You can use the C# code, but you need to add it in a C# project to your Visual Studio solution. Then you can add a reference from your VB.NET project to the C# project with the email class. Take a look at the article attached sample code. There are 2 projects. You need to take the project that is located in Utils folder and add it to your solution.

Dear Florentin,
Thanks for your reply. I think that's the best way to solve the situation. But since I was totally ignorant about that process, I did different thing. I created a new C# DLL project with only the

EnhancedMailMessage.cs

File in it and compiled to a .DLL file. Then in my VB .NET project added the .DLL as REFERENCE and also Imported that in Code file. Since it is working, I'll keep it in this way. But from you I know how to use .NET codes through cross-platform (I mean your solution will work if I want to use some VB .NET code in a C# project, right?).

Glad you were able to find a solution to make it work. It's true, if you don't want to make any changes to the code, adding a reference to a dll is enough. With my solution, you would have been able to change the C# code also.

'''
''' The display name that will appear in the recipient mail client
'''
Public Property FromName() As String
Get
Return m_FromName
End Get
Set(ByVal Value As String)
m_FromName = Value
End Set
End Property

'''
''' SMTP server (name or IP address)
'''
Public Property SMTPServerName() As String
Get
Return m_SMTPServerName
End Get
Set(ByVal Value As String)
m_SMTPServerName = Value
End Set
End Property

'''
''' Username needed for a SMTP server that requires authentication
'''
Public Property SMTPUserName() As String
Get
Return m_SMTPUserName
End Get
Set(ByVal Value As String)
m_SMTPUserName = Value
End Set
End Property

'''
''' Password needed for a SMTP server that requires authentication
'''
Public Property SMTPUserPassword() As String
Get
Return m_SMTPUserPassword
End Get
Set(ByVal Value As String)
m_SMTPUserPassword = Value
End Set
End Property

'''
''' SMTP server port (default 25)
'''
Public Property SMTPServerPort() As Integer
Get
Return m_SMTPServerPort
End Get
Set(ByVal Value As Integer)
m_SMTPServerPort = Value
End Set
End Property

'''
''' If SMTP server requires SSL
'''
Public Property SMTPSSL() As Boolean
Get
Return m_SMTPSSL
End Get
Set(ByVal Value As Boolean)
m_SMTPSSL = Value
End Set
End Property

Public Sub Send()
If m_SMTPServerName.Length = 0 Then
Throw New Exception("SMTP Server not specified")
End If

If FromName.Length > 0 Then
Me.Headers.Add("From", String.Format("{0} ", FromName, From))
End If

' ssl if needed
If SMTPSSL Then
Me.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "True")
End If
System.Web.Mail.SmtpMail.SmtpServer = SMTPServerName
System.Web.Mail.SmtpMail.Send(Me)

It looks like you don’t appreciate critics, even that you proved you learn from them. Yesterday somebody disapprove your the article for being of a null information. You removed it and recreated it again. This is funny – you removed the message too.

BTW: What about using properties in your class. You know, the ones with get {} and set {}. It is not a good standard practice to expose your class variables directly.

Thank you very much for your comment. As the matter of fact I appreciate critics and as you noticed I am willing to learn from them. I followed your suggestions and updated the article and the source code to use properties instead of public members. Please let me know if you have any more suggestions for improvements. Until now I have focused on the functionality, but I realize that the source code must have a good form also.
As for the first draft of the article, I just wanted to updated it but I am not very experienced with the site, being a fairly new member.