• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    ASP.NET Articles

    KB: Sending Emails from ASP.NET 2.0

    SUNDAY, FEBRUARY 21, 2010

    Sending out emails is a common requirement in every web application. This could range from a simple Contact Us form, to daily digest email to all subscribers. In ASP.NET, System.Net.Mail namespace provides all necessary functions of sending emails. In earlier versions of .NET, System.Web.Mail, but the new namespace provides extensive and powerful interfaces for sending out emails than the older alternative.

    Sending emails can be achieved using MailMessage and SmtpClient classes. SmtpClient is used to connect to an SMTP server to send emails, whereas MailMessage is an an object oriented representation for email message. There are other supporting classes like MailAddress, MailAddressCollection, Attachment, AttachmentCollection, AlternateView and LinkedResource.

    MailMessage Class

    From MailAddress object for the email sender
    To MailAddressCollection. This supports addition of multiple addresses in To field.
    CC MailAddressCollection. This supports addition of multiple addresses in Cc field.
    Bcc MailAddressCollection. This supports addition of multiple addresses in BCc field.
    ReplyTo MailAddress. This optional property sets the reply-to field for the email.
    Subject String field, which holds the subject of the email message
    Body String field, to set the body of the email.
    IsBodyHTML Boolean field. If this field is set as true, then the body is send as HTML content, or if it is false, then the message body is plain text. This determines the Content-Type of the email - text/html or text/plain
    Attachments AttachmentCollection. This allows addition of multiple attachments to the email, each being an object of Attachment object.
    Priority Sets the priority of email - High, Normal, Low.

    Create an Email Object

         Dim mail As New MailMessage
        
        mail.From = New MailAddress("fromaddress@someserver.com")
        mail.To.Add("email1@server1.com", "email2@server2.com"
        mail.Subject = "Your email subject here"
        mail.Body = "<p>Write your email content here</p>"
        mail.IsBodyHTML = True
    

    Using MailAddress class for email addresses

    The MailAddress class provides flexibility in declaring email addresses. The MailAddress class has two properties - DisplayName and Address - these can be used to differentiate the actual address and the display name of an email.

         Dim address As New MailAddress(emailAddress, displayName)
        Dim address2 As New MailAddress("mymail@myserver.com","My Name")
    

    The To, Bcc and CC properties of the MailMessage are MailAddressCollection typed properties, which allow addition of multiple MailAddress objects. However, this can be used in the following ways:

         'All emails are provided in a single string
        mail.To.Add("email1@server1.com,email2@server2.com,email3@server3.com")             
                    
        'Each email is provided as seperate string
        mail.To.Add("email1@server1.com", "email2@server2.com", "email3@server3.com")       
        
        'Each email is provided as MailAddress object
        mail.To.Add(New MailAddress("email1@server1.com","Email1"), New MailAddress("email2@server2.com")) 
    

    SmtpClient Class

    The SmtpClient class allows applications to send email by connecting to an SMTP server. This requires SMTP server settings like hostname, port (default is 25), use SSL?, username and password. To send an email using SmtpClient class from any SMTP server, connectivity between the web server and the SMTP server using the above said port should be permitted. The settings can be provided for the SmtpClient by either by code, or by web.config <mailSettings> section.

         <configuration>
            <appSettings/>
            <system.net>
                <mailSettings>
                    <smtp from="default_from_address">
                        <network host="your_smtp_host_name" defaultCredentials="true"/>
                    </smtp>
                </mailSettings>
            </system.net>
        </configuration>
    
    If the SMTP server requires authentication, provide userName and password properties in the network element in the mailSettings, instead of defaultCredentials="true". To send emails using SmtpClient, the following snippet model can be used.
         Dim mail As MailMessage
        '
        ' All other statements as explained earlier
        '
        Dim smtp As New SmtpClient
        smtp.Send(mail)
    

    The above code statements would use the web.config settings as default settings to send email. Alternatively, you can also set the smtp properties from your code. The part of code to do this would look as follows:

         Dim smtp As New SmtpClient(smtp_host_name, port)
        smtp.Credentials = New System.Net.NetworkCredential(userName, password);
        smtp.EnableSSL = True           'Set this as True only if your SMTP server requires an SSL connection.
        smtp.Send(mail)
    

    ConsultSarath - We provide end to end outsourcing solutions for .net programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for php programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for python programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India.

    Other Popular Articles
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...