如何编写发送电子邮件的简单代码

时间:2019-05-10 08:52:03

标签: c# asp.net

我开发了一个简单的网站,该网站存储用户名并将其发送到我的emailid,然后下载文件。文件正在下载但没有通过电子邮件发送给我。

             try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add("mygmailid");
               mailMessage.From =new MailAddress("mydomainbasedemailid");
                mailMessage.Subject = "ASP.NET e-mail test";
   mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
         SmtpClient smtpClient=new SmtpClient("mail.mydomain.com",587);
                smtpClient.Send(mailMessage);
                Response.Write("E-mail sent!");
            }
            catch (Exception ex)
            {
   Response.Write("Could not send the e-mail - error: " + ex.Message);
            }

1 个答案:

答案 0 :(得分:0)

尝试像这样设置Smtp.Credentials:

string HOSTLOGIN = "YourHostLogin";
string HOSTPW = "YourTopSecretPasswort";

var credentials = 
       new System.Net.NetworkCredential() { UserName = HOSTLOGIN, Password = HOSTPW };
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
client.EnableSsl = true;