1
Answer

Sending mail through ASP.NET with C# not working

stelle thomas

stelle thomas

8y
458
1
I am trying to send a keycode to the student through email but it is not sending . Whenever I click on submit button to send the keycode it showed me the message := "At least one of the From or Sender fields is required, and neither was found." but I have already included it in my code.
 
HERE IS MY .cs FILE :
 
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;
using System.Web.Util;
public partial class admin_ExamSchedule : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int examtypeid = Convert.ToInt32(Session["examtypeid"].ToString());
ddlExamIdNotAttempted.DataSource = objAttemptexam.getNotAttemptedExamId(examtypeid);
ddlExamIdNotAttempted.DataValueField = "ExamId";
ddlExamIdNotAttempted.DataTextField = "ExamName";
ddlExamIdNotAttempted.DataBind();
// Response.Write("<script>alert('Must Select Any One')</script>");
//ddlExamIdAttempted.DataSource = objAttemptexam.getAttemptedExamId();
//ddlExamIdAttempted.DataValueField = "ExamId";
//ddlExamIdAttempted.DataTextField = "ExamName";
//ddlExamIdAttempted.DataBind();
}
}
BALexam objAttemptexam = new BALexam();
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
objAttemptexam.uid = Convert.ToInt32(Session["uid"].ToString());
objAttemptexam.ExamReqDate = Convert.ToDateTime(Session["preferdate"]).ToString();
objAttemptexam.ExamAssignDate =txtAssignExamDate.Text;
objAttemptexam.ExamId = Convert.ToInt32(ddlExamIdNotAttempted.SelectedValue);
objAttemptexam.ExamRequestId = Convert.ToInt32(Session["ExamRequestId"].ToString());
// objAttemptexam.UserName = Session["User"].ToString();
objAttemptexam.KeyCode = txtKeyCode.Text;
string strMsg;
SendMails();
int i = objAttemptexam.InsertScheduleExam(out strMsg);
if (i > 1)
{
ClearData();
LblMsg.Text = strMsg;
}
else
LblMsg.Text = strMsg;
}
catch (Exception ex)
{
LblMsg.Text = ex.Message;
}
}
void SendMails()
{
MailMessage objMail = new MailMessage();
objMail.From = "OnLineWebExamination.Com";
objMail.To = "Localhost";
objMail.Subject = "OnLineExam Schedule.";
objMail.Body = "Your Key : " + txtKeyCode.Text + "" + "\n" +
"Your ExamId : " + Convert.ToInt32(ddlExamIdNotAttempted.SelectedValue) + "" + "\n" +
"Your ExamDate : " + txtAssignExamDate.Text;
SmtpMail.SmtpServer = "LocalHost";
SmtpMail.Send(objMail);
Page.RegisterClientScriptBlock("Dhanush", "<script>alert('Key Send to Student Successfully...')</script>");
}
private void ClearData()
{
txtAssignExamDate.Text = txtKeyCode.Text = "";
}
protected void ddlExamIdAttempted_DataBound(object sender, EventArgs e)
{
ddlExamIdNotAttempted.Items.Insert(0, "--Select--");
}
protected void ddlExamIdNotAttempted_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void LB_Click(object sender, EventArgs e)
{
Response.Redirect("frmShowExamRequest.aspx");
}
}
 
HERE IS MY .aspx file :
 
<%@ Page Language="C#" MasterPageFile="~/admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="~/admin/frmExamSchedule.aspx.cs" Inherits="admin_ExamSchedule" Title="Untitled Page" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentBody" Runat="Server">
<center>
<table border="2">
<tr><td style="font-size: x-large" colspan="3">Exam Schedule</td></tr>
<tr><td >ExamId</td><td><asp:DropDownList ID="ddlExamIdNotAttempted" runat="server" Width="150"
onselectedindexchanged="ddlExamIdNotAttempted_SelectedIndexChanged"></asp:DropDownList></td>
<%-- <td><asp:DropDownList ID="ddlExamIdAttempted" runat="server" Width="150"
ondatabound="ddlExamIdAttempted_DataBound"></asp:DropDownList></td>--%>
</tr>
<tr><td >Date Of Assign</td>
<td><asp:TextBox ID="txtAssignExamDate" runat ="server" Width="150px" ></asp:TextBox>
&nbsp;&nbsp;
<cc1:CalendarExtender ID="CE1" runat="server" TargetControlID="txtAssignExamDate">
</cc1:CalendarExtender></tr>
<tr><td >Key Code</td><td><asp:TextBox ID="txtKeyCode" runat="server" Width="150"></asp:TextBox></td></tr>
<tr><td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" Height="31px" />&nbsp;</td></tr>
</table>
<asp:Label ID="LblMsg" runat="server" Text=""></asp:Label>
&nbsp;
<asp:LinkButton ID="LBScheduleExam" runat="server" onclick="LB_Click">Go To
Schedule Exam</asp:LinkButton>
</center>
</asp:Content>
 
 
 

Answers (1)