Unable to send the mailmessage in asp.net

有些话、适合烂在心里 提交于 2019-11-29 18:10:31

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.

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