Blazor C#发送电子邮件

I have a razor component in my Blazor project where when user click a button it will trigger email The button

OnClick功能电子邮件将通过以下代码发送电子邮件

async Task Email(string originalpiv, string docno, string cuscode, string cusname, string docdate)
{
   CModule.SendMail("example@gmail.com", "example@gmail.com", "", "HI", "HI BRO");

}

CModule.SendMail是一种方法,在该方法中,来自,至,主题,正文的电子邮件将通过

public static bool SendMail(string from, string to, string cc, string subject, string body)
        {
            bool rst = false;
            MailMessage Msg = new MailMessage();
            try
            {
                SmtpClient smtp = new SmtpClient("mail.gmail.com", 587);
                string[] toeml = to.Split(new Char[] { ';', ',' });
                foreach (string tmp in toeml)
                {
                    if (tmp.Trim() != "") Msg.To.Add(tmp.Trim());
                }
                string[] cceml = cc.Split(new Char[] { ';', ',' });
                foreach (string tmp in cceml)
                {
                    if (tmp.Trim() != "") Msg.CC.Add(tmp.Trim());
                }
                Msg.From = new MailAddress(from.Trim());
                Msg.Subject = subject;
                Msg.Body = body;
                Msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                smtp.Credentials = new System.Net.NetworkCredential("nick.chau@gmail.com", "880215");
                smtp.Send(Msg);
                rst = true;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                Msg.Dispose();
            }
            return rst;
        }

这些是我尝试过的代码,但是电子邮件没有发送出去,不确定为什么,有什么想法吗?

评论