使用C#通过ASP.NET发送邮件不起作用 [英] Sending mail through ASP.NET with C# not working
问题描述
我正在尝试通过电子邮件向学生发送密码,但它并未发送。每当我点击提交按钮发送密钥代码时,它会向我显示以下消息:=至少需要一个From或Sender字段,而且都找不到。
这里是我的.aspx文件:
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 :
<%@ 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>
<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" /> </td></tr>
</table>
<asp:Label ID="LblMsg" runat="server" Text=""></asp:Label>
<asp:LinkButton ID="LBScheduleExam" runat="server" onclick="LB_Click">Go To
Schedule Exam</asp:LinkButton>
</center>
</asp:Content
>
我尝试过:
这里是我的.cs文件:
>
What I have tried:
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");
}
}
推荐答案
查看行
objMail.From = "OnLineWebExamination.Com";
objMail.To = "Localhost";
正如我在评论中所说的
OnLineWebExamination.Com不是有效的电子邮件地址。该字段的格式应为someone@somesite.com
"OnLineWebExamination.Com" is not a valid email address. That field should be in the format "someone@somesite.com"
收件人也是如此 - 它应该是有效的电子邮件地址。 Localhost不是有效的电子邮件地址
The same is true of the recipient - it should be a valid email address. "Localhost" is not a valid email address
这篇关于使用C#通过ASP.NET发送邮件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!