net core 3.1 send mail

1. Open SHPT

After opening it, he will give you an authorization code. Although the authorization code can be applied again, because it costs money to send a message, it is recommended that you save the authorization code given to you to avoid repeated applications.
Insert picture description here



2. Code

     static void Main(string[] args)
        {
    
    
            MailMessage mailMsg = new MailMessage();//实例化对象
            mailMsg.From = new MailAddress("[email protected]", "xxx");//源邮件地址和发件人
            mailMsg.To.Add(new MailAddress("[email protected]"));//收件人地址
            mailMsg.Subject = "邮件发送测试";//发送邮件的标题
            StringBuilder sb = new StringBuilder();
            sb.Append("测试测试测试测试");
            sb.Append("嘿嘿");
            mailMsg.Body = sb.ToString();//发送邮件的内容
            //指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址)
            SmtpClient client = new SmtpClient();//格式:smtp.126.com  smtp.164.com
            client.Host = "smtp.qq.com";
            //要用587端口
            client.Port = 587;//端口
            client.EnableSsl = true; // 使用 SLL 加密
            client.Credentials = new NetworkCredential("[email protected]", "xxxxx"); // 用户名和测试密码
            //发送邮件
            try
            {
    
    
                client.Send(mailMsg);
            }
            catch (SmtpException ex)
            {
    
    
                Console.WriteLine($"发送失败{ex.Message}");
            }
            Console.WriteLine("邮件已发送,请注意查收!");
            Console.ReadKey();
        }



Three. StringBuilder class

String cannot be modified. A new memory space must be re-applied in memory each time the String class is used. If a large number of string modification operations are required in the program, it will cause a large consumption of memory space, so the StringBuilder class is introduced

method effect
Append() method Add the new string object to the end of the already owned StringBuilder object
AppendFormat(string format, object)方法 Add the text to the end of the StringBuilder object and implement the IFormattable interface
Insert(int index, string value)方法 Insert character text at the specified position index of the StringBuilder object
Remove(int startIndex, int length)方法 Indicates that length characters are removed from the startIndex subscript
Replace(string oldValue, string newValue)方法 Replace all the places equal to oldValue in the string with newValue
Clear() method Clear all content in StringBuilder


references

[1] https://blog.csdn.net/zb756999355/article/details/97414782
[2] https://blog.csdn.net/qq_23931339/article/details/102475595

Guess you like

Origin blog.csdn.net/zhaozhao236/article/details/114978470