Results 1 to 6 of 6

Thread: Sending email from ASP.NET Application

  1. #1
    Join Date
    Feb 2009
    Posts
    12

    Sending email from ASP.NET Application

    Hello!
    I am learning ASP.NET & I want to send an Email from my application.
    What should I keep in my mind for this?

  2. #2
    Join Date
    Feb 2009
    Posts
    12

    Re: Sending email from ASP.NET Application

    Code sniplet on sending email via ASP.NET in html format.Below is code that can be used to send html formatted email using ASP.NET.

    Code:
    <%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %><%@ Import Namespace="System.Web.Mail" %>
    <SCRIPT language=VB runat="server"> 
    Sub Page_load(Sender as Object, E as EventArgs) 
        
    
        If request.form("EmailAddress") = "" Then
            dim strResponse as string = "<h2>Send Email using ASP.NET formatted in HTML</h2>"
            lblMessage.Text = strResponse
        Else
            dim strResponse as string = "You just sent an email message formatted in HTML to:
    <h2>" & request("EmailAddress") & "</h2>"
            lblMessage.Text = strResponse
        End If
        
    End Sub 
        
    Sub btn_Click(sender as Object, e as System.EventArgs)
    
        If request.form("EmailAddress") <> ""
            Dim mail As New MailMessage
            mail.From = "youraddress@domain.com"
            mail.To = request.form("EmailAddress")
            mail.Subject = "Message sent using ASP.NET and CDONTS"
            mail.Body = "HTML Message sent from ASPFree.com using ASP.NET and Cdonts
    <a href='http://aspfree.com/aspnet/email.aspx'>Wonder how this is done?</a>
    
    <a href='http://aspfree.com/aspnet/setupcdonts.aspx'>Wonder How to setup CDONTS?</a>"
            mail.BodyFormat = MailFormat.Html
       SmtpMail.SmtpServer = "LocalServerName"
       SmtpMail.Send(mail)
        End If
    End Sub
    
    </SCRIPT>
    
    <H1 align=center>Sending Email via ASP.NET and CDONTS..</H1><B><A 
    href="/aspnet/setupcdonts.aspx">How do I setup my server to use CDONTS?</A></B> 
    
    
    <A href="/allzips/emaildotnet.zip"><IMG 
    src="http://aspfree.com/images/downloadcode.gif" border=0></A> 
    
    
    <asp:Label id=lblMessage runat="server" BorderColor="#cccccc" 
    BorderStyle="solid" Width="400px" Font-Name="Verdana"></asp:Label>
    <FORM name=form1 method=post runat="server">Email Address:<INPUT 
    style="BACKGROUND-COLOR: #ffffa0" size=30 name=EmailAddress>
    
    <INPUT id=btnSubmit type=submit value="Sending Email with ASP.NET" name=b1 runat="server" OnServerClick="btn_Click"> 
    </FORM>

  3. #3
    Join Date
    Feb 2009
    Posts
    7

    Re: Sending email from ASP.NET Application

    Sending e-mail messages

    To compose an e-mail message in your code, you need to start by creating an instance of the MailMessage class, as shown in the following C# snippet:

    Code:
    MailMessage msg = new MailMessage();
    Be sure to include the System.Web.Mail namespace in your code:

    Code:
    using System.Web.Mail;
    Once the object is instantiated, the various properties of the MailMessage class are used per your application. The following lines set the recipient, sender, and subject of the message:

    Code:
    msg.To = "test@test.com";
    msg.From = "me@test.com";
    msg.Subject = "Test Message";
    The next step is setting our mail server via the SmtpServer object's SmtpServer property:

    Code:
    SmtpMail.SmtpServer = "smtp server name or address";
    The final step is sending the message by passing our MailMessage object to the SmtpMail object's Send method:

    Code:
    SmtpMail.Send(msg);
    The previous code used C#. Here's a more complete listing via a Web form's Page_Load event coded in VB.NET:

    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    Handles MyBase.Load
    Dim msg As MailMessage = New MailMessage()
    msg.To = "test@test.com"
    msg.From = "me@test.com"
    msg.Subject = "Test message"
    msg.Body = "Message body"
    Try
    SmtpMail.SmtpServer = " smtp server name or address "
    SmtpMail.Send(msg)
    Catch ex As HttpException
    Response.Write("Error: " + ex.ToString())
    Catch ex As Exception
    Response.Write("Error: " + ex.ToString())
    End Try
    End Sub
    Notice a try/catch block is used to catch any exceptions raised during e-mail message composition. The equivalent C# code follows:

    Code:
    private void Page_Load(object sender, System.EventArgs e) {
    MailMessage msg = new MailMessage();
    msg.To = "test@test.com";
    msg.From = "me@test.com";
    msg.Subject = "Test message";
    msg.Body = "Body of the message";
    try {
    SmtpMail.SmtpServer = "smtp server name or address";
    SmtpMail.Send(msg);
    } catch (HttpException ex) {
    Response.Write("HTTP Error: " + ex.ToString());
    } catch (Exception ex) {
    Response.Write("Error: " + ex.ToString());
    } }

  4. #4
    Join Date
    Feb 2009
    Posts
    5

    Re: Sending email from ASP.NET Application

    Please visit this webpage here you will find out the code you need, which you can also download!
    http://www.codeproject.com/KB/aspnet...plication.aspx
    http://www.4guysfromrolla.com/webtec...tml#postadlink
    http://www.sitepoint.com/article/sen...email-asp-net/
    Hope this helps!

  5. #5
    Join Date
    Feb 2009
    Posts
    133

    Re: Sending email from ASP.NET Application

    Try this code!

    Code:
    <% @Page Language="C#" %>
    <% @Import Namespace="System.Web.Mail" %>
    <%
    string strTo = "christophw@forum.techarena.Com";
    string strFrom = "webmaster@techarena.com";
    string strSubject = "Hi Jack";
    
    SmtpMail.Send(strFrom, strTo, strSubject,
      "A real nice body text here");
    
    Response.Write("Email was queued to disk");
    %>
    What actually happens in the script? The complete email support resides in the System.Web.Mail namespace. In this namespace we find the class SmtpMail, whose static Send method can accept four parameters:

    Code:
    SmtpMail.Send(From, To, Subject, BodyText);
    IF YOU CAN'T Convince THEM ...

    CONFUSE THEM ...

  6. #6
    Join Date
    Feb 2009
    Posts
    7

    Re: Sending email from ASP.NET Application

    Please have a look at this webpage too!
    http://www.wwwcoder.com/parentid/456...2/default.aspx

Similar Threads

  1. Application for sending email from iphone
    By Gunwant in forum Portable Devices
    Replies: 4
    Last Post: 16-02-2010, 11:24 AM
  2. Sending email periodically
    By Aaliya Seth in forum Windows Software
    Replies: 5
    Last Post: 31-12-2009, 10:35 AM
  3. Sending email to multiple recipients from my vb.net application?
    By SushmitaP in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 08:35 PM
  4. PHP form and sending via email
    By Gyan Guru in forum Guides & Tutorials
    Replies: 3
    Last Post: 14-11-2008, 12:21 PM
  5. bellsouth.net email not sending
    By Dil-Ber in forum Windows Vista Mail
    Replies: 3
    Last Post: 21-08-2008, 12:47 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,304,280.60258 seconds with 17 queries