Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2009
Member
 
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?
Reply With Quote
  #2  
Old 13-02-2009
Member
 
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>
Reply With Quote
  #3  
Old 13-02-2009
Member
 
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());
} }
Reply With Quote
  #4  
Old 13-02-2009
Member
 
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!
Reply With Quote
  #5  
Old 13-02-2009
Member
 
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 ...
Reply With Quote
  #6  
Old 13-02-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 07:57 AM.