.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

email using dot net

Posted By: mamallan     Posted Date: January 03, 2011    Points:2   Category :C#
i entered these but i didnt got output
<%@ Page Language="C#" %>
<%@ Import NameSpace="System.Web.Mail" %>
<%@ Import Namespace="System.Web.Mail.SmtpMail" %>

<script runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if(!Page.IsPostBack)
Message.Text = "Sending Email using ASP.NET";
}

protected void Send_Email(Object Sender, EventArgs e)
{

MailMessage mail = new MailMessage();

mail.To = Request.Form["to"];
mail.From = Request.Form["from"];

if(Request.Form["format"].Equals("text"))
mail.BodyFormat = MailFormat.Text;
else
mail.BodyFormat = MailFormat.Html;

mail.Subject = Request.Form["subject"];
mail.Body = Request.Form["body"];



SmtpMail.Send(mail);
Response.Flush();

Message.Text = "Message Sent...<br><br>" +
"<a href=\"default.aspx\">Go Back</a>";
}
</script>

<html>
<head>
<title>Sending Emails using ASP.NET ( Part I )</title>
<style>
a { font-size:8pt; font-family:Tahoma; font-weight:normal;
color:maroon; }
</style>
<link rel="stylesheet" href="css/demostyle.css">
</head>
<body bgcolor="white">

<p align="center" class="title">
<asp:label id="Message" runat="server" />
</p>

<% if(!Page.IsPostBack) { %>
<p align="center">All fields are required.</p>

<form id="Form1" method="post" runat="server">
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="20%" align="right">From : </td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<td align="right">To : </td>
<td><input type="text" name="to"></td>
</tr>
<tr>
<td align="right">MailFormat : </td>
<td>
<select name="format">
<option value="text">Text</option>
<option value="html">HTML</option>
</select>
</td>
</tr>
<tr>
<td align="right">Subject : </td>
<td><input type="text" name="subject" style="width:400;"></td>
</tr>
<tr>
<td valign="top" align="right">Body : </td>
<td>
<textarea cols="5" rows="10" name="body" style="width:400;">
</textarea></td>
</tr>
<tr>
<td> </td>
<td>
<input id="Submit1" type="submit" OnServerClick="Send_Email" runat="server"
class="submit" value=" " /> Send Email!
</td>
</tr>
</table>
</form>
<% } %>

</body>
</html>

Responses
Author: Karthikeyan Anbarasan       [Moderator]      
Posted Date: January 03, 2011     Points: 5   

Use the below code for sending mail.


MailMessage msgMail = new MailMessage();

msgMail.To = "christophw@sleeper.Dev.AlfaSierraPapa.Com";
msgMail.From = "webmaster@aspheute.com";
msgMail.Subject = "Attachment Test";

msgMail.BodyFormat = MailFormat.Text;
msgMail.Body = "Check out the attachment!";
msgMail.Attachments.Add(new MailAttachment("c:\\Document.pdf"));

SmtpMail.Send(msgMail);

Response.Write("Email was sent");



Thanks
Karthik
http://www.f5debug.net
Mark as Answer if the answer solves your issue!!!
Author: Dhiraj Ranka             
Posted Date: January 04, 2011     Points: 5   

Try this code


NameValueCollection settings;
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
settings = System.Configuration.ConfigurationManager.AppSettings;

string UN = settings["UserName"];
string PASS = settings["Password"];
string SMTP = settings["MailServer"];
int PORT = Convert.ToInt32(settings["MailServerPort"]);
foreach (string str in sendto)
{
body = "Your body text goes here";
MailMessage msg = new MailMessage();
msg.From = new MailAddress(UN);
msg.To.Add(new MailAddress(str));
msg.Subject = "NX27K Alert - " + DateTime.Now.ToShortDateString();
msg.Body = body;
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = SMTP;
smtp.Port = PORT;
smtp.Credentials = new System.Net.NetworkCredential(UN, PASS);
smtp.EnableSsl = true;
smtp.Send(msg);
}


Hope this helps.

Thanks & Regards,
Dhiraj Ranka
http://www.twitter.com/dhirajranka | http://www.dhirajranka.com


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend