. net sending mail problems and Solutions

Time:2020-11-27

. net background code using QQ mailbox server to send mail encountered problems:”mail from address must be same as authorization user” 

First, take a look at my code implementation

Background code:

public class EmailHelper
  {
    /// <summary>
    ///Send mail
    /// </summary>
    ///< param name = "subject" > subject < / param >
    ///< param name = "body" > content < / param >
    public static void SendEmail(string subject,string body)
    {
      var mailFrom = ConfigurationManager.AppSettings["mailFrom"];
      var mailTo = ConfigurationManager.AppSettings["mailTo"];
      var mailCC = ConfigurationManager.AppSettings["mailCC"];
      var smtp = ConfigurationManager.AppSettings["smtp"];
      var smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
      var emailAccount = ConfigurationManager.AppSettings["emailAccount"];
      var emailPassword = ConfigurationManager.AppSettings["emailPassword"];

       //Message content information
      MailMessage mailMessage = new MailMessage
      {
        From = new MailAddress(mailFrom),
        Subject = subject,
        Body = body,
      };
      mailMessage.To.Add(mailTo);
      mailMessage.CC.Add(mailCC);

       //Mailbox server configuration
      SmtpClient smtpClient = new SmtpClient(smtp, smtpPort)
      {
        Credentials = new NetworkCredential(emailAccount, emailPassword),
        EnableSsl = true,
      };

       //Send mail
      smtpClient.Send(mailMessage);
    }
  }

Configuration file:

<appSettings>
  <! -- sender, recipient, CC -- >
  <add key="mailFrom" value="[email protected]"/>
  <add key="mailTo" value="[email protected]"/>
  <add key="mailCC" value="[email protected]"/>

  <! -- mailbox server host address and port number -- >
  <add key="smtp" value="smtp.qq.com"/>
  <add key="smtpPort" value="25"/>

  <! -- sender's email account and password -- >
  <add key="emailAccount" value="53xxx1314"/>
  <! -- this password is not a QQ password, but a password generated by the mailbox -- >
  <add key="emailPassword" value="stxxxxxxxxtscbbb"/>
 </appSettings>

The code is done. All that’s left is to run

The first time I run into a problem is what I said at the beginning“mail from address must be same as authorization user”

Specific why can be like this, my account password is right ah, read a few articles of Bo you, confirm is QQ mailbox password problem. However, it is normal for me to log in to the mailbox with QQ password, and then try to open the pop / SMTP service and find the main problem

The pop / SMTP service of QQ mailbox is not enabled

Operation steps:

Go to QQ email > Settings > account > scroll to“POP3 / imap / SMTP / exchange / carddav / caldav services

If it’s not turned on, turn it on

I didn’t open it. I need to send a text message after clicking,Configure mail client

After sending SMS, I got the authorization code, replaced the QQ password I wrote in the code with the authorization code, and then ran again. The error disappeared, mainly due to the unauthorized problem

Note that the authorization code will be invalid after the QQ password or independent password is modified and needs to be obtained again.

Re run, no problem

Although the problem is not very big, and there is not much technical content, it can save us from encountering this problem again, but we can’t remember how to solve it. In retrospect, it will save time than paying attention to it again.

All of the above, I hope that all of the above will be helpful to you.