smtp exception Failure sending mail?

前端 未结 5 507
灰色年华
灰色年华 2021-01-05 01:03
StringBuilder emailMessage = new StringBuilder();
emailMessage.Append(\"Dear Payment Team ,\");
emailMessage.Append(\"

Please find the Payment ins
相关标签:
5条回答
  • 2021-01-05 01:26

    Another reason for this exception could be the anti virus installed on your system. This may be prohibiting the application to send mails. Just look out for a dialog box that is asking for permission to send mails through an application on your system.

    0 讨论(0)
  • 2021-01-05 01:27

    I suggest you the simplest way to send email on exception - using Elmah. Please follow this guide line for the solution: https://www.stormconsultancy.co.uk/blog/development/tools-plugins/setup-email-alerts-from-elmah-when-exceptions-are-raised/

    0 讨论(0)
  • 2021-01-05 01:28

    First, you don't need to manually read the values in your .config file. You can set the in the System.Net section and your SmtpClient object will read them automatically:

    <system.net>
        <mailSettings>
          <smtp from="Sender's display name &lt;sender_email@domain.com&gt;">
            <network host="mailserver.yourdomain.com" port="25" userName="smtp_server_username" password="secret" defaultCredentials="false" />
          </smtp>
        </mailSettings>
      </system.net>
    

    Then, from your code, you just write:

            SmtpClient smtp = new SmtpClient();
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(new MailAddress("recipient@somedomain.com", "Recipient Display Name"));
            mailMessage.Subject = "Some Subject";
            mailMessage.Body = "One gorgeous body";
            smtp.Send(mailMessage);
    

    Coming back to your error, it would appear you have some kind of a network problem.

    0 讨论(0)
  • 2021-01-05 01:44

    You need to give Username and password for smtp.

    Use Below code :-

        MailSettings.SMTPServer = Convert.ToString(ConfigurationManager.AppSettings["HostName"]);
        MailMessage Msg = new MailMessage();
        // Sender e-mail address.
        Msg.From = new MailAddress("pqr@gmail.com");
        // Recipient e-mail address.
        Msg.To.Add("abc@gmail.com");
        Msg.CC.Add("zcd@gmail.com");
        Msg.Subject = "Timesheet Payment Instruction updated";
        Msg.IsBodyHtml = true;
        Msg.Body = emailMessage.ToString();
        NetworkCredential loginInfo = new NetworkCredential(Convert.ToString(ConfigurationManager.AppSettings["UserName"]), Convert.ToString(ConfigurationManager.AppSettings["Password"])); // password for connection smtp if u dont have have then pass blank
    
        SmtpClient smtp = new SmtpClient();
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = loginInfo;
        //smtp.EnableSsl = true;
        //No need for port
        //smtp.Host = ConfigurationManager.AppSettings["HostName"];
        //smtp.Port = int.Parse(ConfigurationManager.AppSettings["PortNumber"]);
         smtp.Send(Msg);
    
    0 讨论(0)
  • 2021-01-05 01:49
    try
               {
                   MailMessage mail = new MailMessage();
                   //SmtpClient SmtpServer = new SmtpClient("smtp.google.com");
                  SmtpClient SmtpServer = new SmtpClient(sServer);
    
                  // SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
                   mail.From = new MailAddress(sFromEmailId);
                   mail.To.Add(sToEmailId);
                   mail.Subject = sSubject;
                   mail.Body = sMessage;
                   mail.IsBodyHtml = true;             
                   HttpFileCollection hfc = Request.Files;
                   for (int i = 0; i < hfc.Count; i++)
                   {
                       HttpPostedFile hpf = hfc[i];
                       if (hpf.ContentLength > 0)
                       {
                           mail.Attachments.Add(new Attachment(fupload.PostedFile.InputStream, hpf.FileName));
    
                       }
                   }
                   SmtpServer.Port = 587;
                   //SmtpServer.Host = "smtp.gmail.com";
                   SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                   SmtpServer.UseDefaultCredentials = false;
                   SmtpServer.Credentials = new System.Net.NetworkCredential(sFromEmailId, sPassword);
                   SmtpServer.EnableSsl = true;
                   SmtpServer.Send(mail);                
                   ClientScript.RegisterStartupScript(this.GetType(), "Alert", "dim('Mail Sent Successfully..!');", true);
                   mail.Dispose();
               }
               catch (Exception ex)
               {                
                   ClientScript.RegisterStartupScript(this.GetType(), "Alert", "dim('Error in Sending Mail..!');", true);
               }
    
    0 讨论(0)
提交回复
热议问题