2016-03-19 105 views
0

我试图通过电子邮件发送密钥码,但是每次发生此错误时: - “服务器拒绝发件人地址。服务器响应为:530 5.7.0必须首先发出STARTTLS命令。 e6sm29167643pfj.71-gsmtp.“。我认为问题是与SendMails(),但我没有得到什么问题。请帮助我out.I使用Visual Studio社区2015年?错误: - 服务器拒绝发件人地址。服务器响应是:530 5.7.0必须首先发出STARTTLS命令。 e6sm29167643pfj.71 - gsmtp

下面是代码: -

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 = "[email protected]"; 
    objMail.To = "[email protected]"; 
    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

只是检查您的错误信息,它明确指出了问题:你不使用安全连接和Gmail不允许发送不安全的连线。

顺便说一句:SmtpMail类不推荐使用,建议使用System.Net.SmtpClient代替。

相关问题