我正在尝试通过电子邮件发送密钥代码,但每次我收到此错误: - “服务器拒绝了发件人地址。服务器响应为:530 5.7.0必须首先发出STARTTLS命令.e6sm29167643pfj。 71 - gsmtp。“。我认为问题出在SendMails()上,但我没有得到那个问题。请帮帮我。我正在使用Visual Studio社区2015?
以下是代码: -
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 = "abc27@gmail.Com";
objMail.To = "def4444@gmail.com";
objMail.Subject = "OnLineExam Schedule.";
objMail.BodyFormat = MailFormat.Html;
objMail.Body = "Your Key : " + txtKeyCode.Text + "" + "\n" +
"Your ExamId : " + Convert.ToInt32(ddlExamIdNotAttempted.SelectedValue) + "" + "\n" +
"Your ExamDate : " + txtAssignExamDate.Text;
SmtpMail.SmtpServer = "smtp.gmail.com";
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");
}
}
答案 0 :(得分:0)
只需检查您的错误消息,它就会明确指出问题:您没有使用安全连接,Gmail不允许通过不安全的连接发送。
顺便说一下:不推荐使用SmtpMail类,建议使用System.Net.SmtpClient。