Sending mail using SmtpClient in .net

前端 未结 5 1447
一整个雨季
一整个雨季 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 02:35

    I figured out that setting the SmtpClient Credentials property before setting the ' UseDefaultCredentials = false ' UseDefaultCredentials = false causes the credentials to be ignored.

    Fails:

    SmtpClient smtp = new SmtpClient;
    smtp.Credentials = new NetworkCredential("user","pass");
    smtp.UseDefaultCredentials = false;
    

    Works:

    SmtpClient smtp = new SmtpClient;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("user","pass");
    

    Go figure.

    ** UPDATE **

    The reason the order is important here is that the setter on the UseDefaultCredentials property actually sets the Credentials to null via the decompiled code:

    /// <summary>Gets or sets a <see cref="T:System.Boolean" /> value that controls whether the <see cref="P:System.Net.CredentialCache.DefaultCredentials" /> are sent with requests.</summary>
    /// <returns>
    /// <see langword="true" /> if the default credentials are used; otherwise <see langword="false" />. The default value is <see langword="false" />.</returns>
    /// <exception cref="T:System.InvalidOperationException">You cannot change the value of this property when an e-mail is being sent.</exception>
    public bool UseDefaultCredentials
    {
      get
      {
        return this.transport.Credentials is SystemNetworkCredential;
      }
      set
      {
        if (this.InCall)
          throw new InvalidOperationException(SR.GetString("SmtpInvalidOperationDuringSend"));
        this.transport.Credentials = value ? (ICredentialsByHost) CredentialCache.DefaultNetworkCredentials : (ICredentialsByHost) null;
      }
    }
    
    0 讨论(0)
  • 2021-02-07 02:38

    It means your mail server does not allow Mail-Relay. Your mail server only allows you to send mail from authenticated email-id as username. Generally this is done to prevent mails being sent as different identities other than the authenticated one.

    0 讨论(0)
  • 2021-02-07 02:43

    Try this :

    MailMessage mail = new MailMessage("emailfrom","emailto");
    
    mail.From = new MailAddress("emailfrom");
    mail.Subject = txtsbjct.Text;
    string Body = txtmsg.Text;
    mail.Body = Body;
    
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
    smtp.Credentials = new System.Net.NetworkCredential("youremail", "yourpassword");
    
    smtp.EnableSsl = true;
    smtp.Send(mail);
    txtemail.Text = "";
    txtmsg.Text = "";
    txtsbjct.Text = "";
    Label1.Text = "your email has been send";
    mail = null;
    smtp = null;
    
    0 讨论(0)
  • 2021-02-07 02:50

    The short answer : Do not use .net the internal SMTP client class - use MailKit.

    The long answer :

    1. It is marked as obsolete in MS docs

    [System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")] public class SmtpClient : IDisposable

    1. Use the oss lib MailKit.
    2. Elaboration : Because you will face its deficiencies sooner rather than later. Read here or read around the internet.
    3. Here is an example for sending plain messages and multipart messages.

    I intentionally did not copy-paste the examples from the docs here because the documentation may change over time and it is better to read .

    0 讨论(0)
  • 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;
           }
       }
    
    0 讨论(0)
提交回复
热议问题