Unable to send the mailmessage in asp.net

我的未来我决定 提交于 2019-11-28 12:52:39

问题


The following code unable to send an emails to customers and it not throwing any exception. The code it not send any email or exception but executed.I am completely new about the asp.net. Some one can help me how to resolve the problem.

Code:

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);
}

回答1:


You have to set smtp.EnableSsl=true and use port number 587. Your final code will be this:

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);
}

I tested this code with my credentials and it works fine.




回答2:


 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);


来源:https://stackoverflow.com/questions/25543233/unable-to-send-the-mailmessage-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!