Sending mail using SmtpClient in .net

前端 未结 5 1440
一整个雨季
一整个雨季 2021-02-07 02:19

I am unable to send the mail using smtp client. here is the code:

SmtpClient client=new SmtpClient(\"Host\");
client.Credentials=new NetworkCredential(\"username         


        
5条回答
  •  礼貌的吻别
    2021-02-07 03:00

    void sendEmail(string strFrom
                            , string strTo
                            , string strSubject
                            , string strBody)
       {
    
           MailMessage objMailMessage = new MailMessage();
           System.Net.NetworkCredential objSMTPUserInfo =
               new System.Net.NetworkCredential();
           SmtpClient objSmtpClient = new SmtpClient();
    
           try
           {
               objMailMessage.From = new MailAddress(strFrom);
               objMailMessage.To.Add(new MailAddress(strTo));
               objMailMessage.Subject = strSubject;
               objMailMessage.Body = strBody;
    
               objSmtpClient = new SmtpClient("172.0.0.1"); /// Server IP
               objSMTPUserInfo = new System.Net.NetworkCredential
               ("User name", "Password","Domain");
               objSmtpClient.Credentials = objSMTPUserInfo;
               objSmtpClient.UseDefaultCredentials = false;
               objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    
               objSmtpClient.Send(objMailMessage);
           }
           catch (Exception ex)
           { throw ex; }
    
           finally
           {
               objMailMessage = null;
               objSMTPUserInfo = null;
               objSmtpClient = null;
           }
       }
    

提交回复
热议问题