Hello!
I am learning ASP.NET & I want to send an Email from my application.
What should I keep in my mind for this?
Hello!
I am learning ASP.NET & I want to send an Email from my application.
What should I keep in my mind for this?
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>
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:
Be sure to include the System.Web.Mail namespace in your code:Code:MailMessage msg = new MailMessage();
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:using System.Web.Mail;
The next step is setting our mail server via the SmtpServer object's SmtpServer property:Code:msg.To = "test@test.com"; msg.From = "me@test.com"; msg.Subject = "Test Message";
The final step is sending the message by passing our MailMessage object to the SmtpMail object's Send method:Code:SmtpMail.SmtpServer = "smtp server name or address";
The previous code used C#. Here's a more complete listing via a Web form's Page_Load event coded in VB.NET:Code:SmtpMail.Send(msg);
Notice a try/catch block is used to catch any exceptions raised during e-mail message composition. The equivalent C# code follows: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
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()); } }
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!
Try this code!
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:<% @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"); %>
Code:SmtpMail.Send(From, To, Subject, BodyText);
IF YOU CAN'T Convince THEM ...
CONFUSE THEM ...
Please have a look at this webpage too!
http://www.wwwcoder.com/parentid/456...2/default.aspx
Bookmarks