Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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."




HERE IS MY .aspx file :

ASP.NET
<%@ 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 align="left">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 align="left">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 align="left">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

>

What I have tried:

HERE IS MY .cs FILE :

C#
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");
    }
}
Posted
Updated 17-Mar-16 4:15am
Comments
CHill60 17-Mar-16 10:01am    
"OnLineWebExamination.Com" is not a valid email address. That field should be in the format "someone@somesite.com"
Member 12170781 17-Mar-16 10:07am    
I changed it to OnlineWebExamination@gmail.com....and now the new error is := "At least one recipient is required, but none were found."
ZurdoDev 17-Mar-16 10:25am    
Because there is no To email address. Think of it as an email you open up. This is very simple for you to fix on your own.

1 solution

Look at the lines
objMail.From = "OnLineWebExamination.Com";
objMail.To = "Localhost";
As I said in my comment
Quote:
"OnLineWebExamination.Com" is not a valid email address. That field should be in the format "someone@somesite.com"

The same is true of the recipient - it should be a valid email address. "Localhost" is not a valid email address
 
Share this answer
 
Comments
Member 12170781 18-Mar-16 2:27am    
sorry for the late reply.... I made changes as you said. I made change in objMail.To="thomastella4444@gmail.com".....and now the new error is 'The transport failed to connect to the server.'
CHill60 18-Mar-16 5:43am    
Think about it ... the first problem was because you hadn't set up .From correctly.
The next problem was because you hadn't set up .To properly ...
Walk through your code and make sure everything is being set to a sensible value. Is "LocalHost" an appropriate setting for an SMTP server?
Have a look at these articles - Sending Email from ASP.NET using Formatted Text Editor and Attachments[^]
How to send email from C#[^]
and try to follow the reasons WHY certain values are assigned to certain properties

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900