1

我创建了一个应用程序,因为我可以选择发送邮件。直到昨天它都正常工作。但是今天我在发送诸如“发送邮件失败”之类的邮件时遇到错误。为此,我在下面给出了我的代码,请帮助我更正它。

 protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (TextBox1.Text == "")
            {

                string alertmessage = "";
                alertmessage = "Email ID. cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox1.Focus();
            }
            else if (TextBox2.Text == "")
            {

                string alertmessage = "";
                alertmessage = "CC To cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox2.Focus();
            }
            else if (TextBox3.Text == "")
            {

                string alertmessage = "";
                alertmessage = "Subject cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox3.Focus();
            }
            else if (TextBox4.Text == "")
            {

                string alertmessage = "";
                alertmessage = "Message Body cannot be blank ";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox4.Focus();
            }
            //else if (upSignature.FileName == "")
            //{
            //    ctr = 1;
            //    string alertmessage = "";
            //    alertmessage = "Attachment  Missing...";
            //    this.CreateMessageAlert(this, alertmessage, "alertKey");
            //    upSignature.Focus();
            //}

            else
            {


                string photo = "Enquiry" + Session["MRNO"].ToString() + FileUpload1.FileName;
                string strpath = Request.MapPath("~/");
                FileUpload1.SaveAs(strpath + "/Enquiry/" + photo);


                try
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(new MailAddress(TextBox1.Text.Trim()));
                    mail.From = new MailAddress("purchase@oeg.co.in");
                    mail.Subject = "Enquiry for MRNO " + " " + " " + Session["MRNO"].ToString() + " " + " " + "Reg.";
                    mail.CC.Add(TextBox2.Text.Trim());
                    mail.Body = TextBox4.Text.Trim();

                    mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
                    mail.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
                    //Attachment attach = new Attachment(strpath + "/Enquiry/" + photo);
                    //mail.Attachments.Add(attach);


                    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

                    smtp.EnableSsl = true;

                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("purchase@oeg.co.in", "xxxxx");
                    //smtp.Credentials = new NetworkCredential("purchaseoeg", "xxxxx");
                    smtp.Send(mail);

                    string alertmessage = "";
                    alertmessage = "Mail Has Been Sent";
                    this.CreateMessageAlert(this, alertmessage, "alertKey");


                    //   Page.RegisterStartupScript("close", "<script language=javascript>self.close();</script>");

                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
        catch (Exception ex1)
        {
            Response.Write(ex1.Message);
        }

    }
4

2 回答 2

0

如果它以前可以工作,而现在不行,那么它很可能是 SMTP 服务器。

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\TestMailMessages\" />
    </smtp>
  </mailSettings>
</system.net>

将上述代码放在您的 App.config 或 Web.config 中。当您发送消息时(通过 var smtpClient = new SmtpClient();),它现在将作为文件存储在“ pickingDirectoryLocation ”目录中。

如果消息可以到达那里,那么您的代码就可以了

然后您可能会继续测试您的 smtp 设置/测试服务器。有很多方法可以做到这一点,包括使用 Telnet。

如果问题是间歇性的,可能会向 SMTP 发送添加重试逻辑。

于 2012-08-13T11:47:19.037 回答
0
public string btnSendmail( )
{
    try
    {
        //Code for send Email
        string msg = txtEmail.Text;
        MailMessage sendMailforSA = new MailMessage();
        SmtpClient smtpforSA = new SmtpClient();
        string subjectforSA = null;
        subjectforSA = "Thanks for apply";
        System.Net.NetworkCredential credforSA = new System.Net.NetworkCredential("yourGmailID@gmail.com", "password");
        sendMailforSA.To.Add("ToEmailID");

        sendMailforSA.From = new MailAddress("yourGmailID@gmail.com");
        sendMailforSA.Subject = subjectforSA.ToString();
        sendMailforSA.Body = "hiii This is Test Message";
        sendMailforSA.IsBodyHtml = false;
        smtpforSA.Host = "smtp.gmail.com";
        smtpforSA.Port = 587;
        smtpforSA.EnableSsl = true;
        smtpforSA.UseDefaultCredentials = false;
        smtpforSA.Credentials = credforSA;
        smtpforSA.Send(sendMailforSA);
        return "Email successfully sent.";
    }
    catch (Exception ex)
    {
        return  "Send Email Failed." + ex.Message;
    }
} 

还要写在 web.config 文件中:

<system.net>
    <mailSettings>
        <smtp from="yourGmailID@gmail.com">
            <network host="smtp.gmail.com" port="587" userName="yourGmailID@gmail.com"    password="password" defaultCredentials="false"/>
        </smtp>
    </mailSettings>
</system.net>
于 2012-08-13T12:10:58.067 回答