【问题标题】:Unable to send the mailmessage in asp.net无法在 asp.net 中发送邮件
【发布时间】:2014-10-21 23:41:36
【问题描述】:

以下代码无法向客户发送电子邮件,并且没有抛出任何异常。它不发送任何电子邮件或异常但已执行的代码。我对 asp.net 完全陌生。有人可以帮我解决这个问题。

代码:

try
{
    String userName = "ramesh";
    String passWord = "123456";
    String sendr = "ramesh@gmail.com";
    String recer = "customer@yahoo.com";
    String subject = "Comformation ";
    String body = "Dear Customer";

    MailMessage msgMail = new MailMessage(sendr, recer, subject, body);

    int PortNumber = 25;
    SmtpClient smtp = new SmtpClient("smtp.test.com", PortNumber);
    msgMail.IsBodyHtml = true;                                     
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Credentials = new System.Net.NetworkCredential(userName, passWord);

    smtp.Send(msgMail);

    MsgLP.Text = "Emailed to Customer..";
    LogInLink.Visible = true;
}
catch (Exception ex){
    AuditLog.LogError("ErrorE-mail " + ex.Message);
}

【问题讨论】:

  • 您查看过垃圾邮件吗?邮件服务器是否有任何安全设置?
  • 您能告诉我如何检查邮件服务器上的垃圾邮件安全设置吗?
  • 不扔怎么知道不发邮件?我建议您的代码可以正常工作,但是一路上的 smtp 服务器没有发送电子邮件。
  • 我的意思是在您发送电子邮件的雅虎邮件的垃圾邮件文件夹中。您使用的是自己的邮件服务器还是第三方。如果您在办公室,您可以从网络部门确认。
  • @Ben Robinson,我将在哪里配置 SmtpClient 服务器以及如何检查服务器状态

标签: c# asp.net .net mailmessage


【解决方案1】:
 System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
        mm.From = new MailAddress("email@gmail.com");
        mm.To.Add("email@gmail.com");
        System.Net.Mail.Attachment attachment;
        string strFileName;
        strFileName = "Uploadfile/" + "200814062455PM_Admin_Screenshot (10).JPEG";
        attachment = new System.Net.Mail.Attachment(Server.MapPath(strFileName));
        mm.Attachments.Add(attachment);
        mm.Body = ("<html><head><body><table><tr><td>Hi</td></tr></table></body></html><br/>"); ;

        mm.IsBodyHtml = true;
        mm.Subject = "Candidate " + Name + "  for your Requirement " + Jobtt + " ";
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        object userstate = mm;
        client.Send(mm);

【讨论】:

    【解决方案2】:

    您必须设置smtp.EnableSsl=true 并使用端口号587。你的最终代码是这样的:

    try
    {
    String userName = "ramesh";
    String passWord = "123456";
    String sendr = "ramesh@gmail.com";
    String recer = "customer@yahoo.com";
    String subject = "Comformation ";
    String body = "Dear Customer";
    
    MailMessage msgMail = new MailMessage(sendr, recer, subject, body);
    
    int PortNumber = 587; //change port number to 587
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", PortNumber); //change from test to gmail
    smtp.EnableSsl = true; //set EnableSsl to true
    msgMail.IsBodyHtml = true;                                     
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Credentials = new System.Net.NetworkCredential(userName, passWord);
    smtp.Send(msgMail);
    MsgLP.Text = "Emailed to Customer..";
    LogInLink.Visible = true;
    }
    catch (Exception ex){
    AuditLog.LogError("ErrorE-mail " + ex.Message);
    }
    

    我用我的凭据测试了这段代码,它运行良好。

    【讨论】:

    • 对不起,我已经在记事本中编辑了代码,它可能是逗号,“smtp.EnableSsl = true;”是什么意思可以给我打电话吗。
    • SSL(安全套接字层)是用于在 Web 服务器和浏览器之间建立加密链接的标准安全技术。更多详情见MSDN
    • “Web.config”文件中的一个澄清网络主机端口是25,现在我将使用587后它是否可以工作?
    • 您可以自己查看。尝试使用25 并检查它是否发送。如果没有发送,请尝试587
    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 2013-06-27
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多