无法发送电子邮件(smtp)

时间:2016-05-29 07:36:44

标签: c# asp.net

我正在完成我的作业,我必须在点击按钮后发送电子邮件验证"验证我"。

下面是按钮和文本框:

button and textbox

所以,流程是,您必须输入您的studentID,然后系统将检查数据库是否存在您的ID。如果您的studentID存在,那么我们将发送确认(随机密码访问网络)。

但在这里,我很难发送电子邮件,老实说,我不知道出了什么问题。

这是我的代码部分(仅发送邮件代码):

public void sendingverification()
    {
        //get EMAIL from spesific ID 
        string getEmail = "SELECT Email FROM TableStudent where StudentID = '" + IdTxt.Text + "'";
        SqlCommand sqlcomEmail = new SqlCommand(getEmail, con);

        //get STUDENT FIRST NAME from spesific ID
        string getName = "SELECT StudentFName FROM TableStudent where StudentID = '" + IdTxt.Text + "'";
        SqlCommand sqlcomName = new SqlCommand(getName, con);

        string randomPw = System.Web.Security.Membership.GeneratePassword(10, 5);
        string activationUrl = "http://aws.prominensa.com/izari/web-alumni/login.html";
        var fromAddress = new MailAddress("AlumnaeTeam@gmail.com", "S2 Alumnae Team");
        var toAddress = new MailAddress(sqlcomEmail.ToString(), sqlcomName.ToString());
        const string fromPassword = "123456";
        const string subject = "Activation Link for S2 Alumnae";
        string body = "Hello " + sqlcomName.ToString() + ", ";
        body += "<br /><br />your account is almost done. You can login with your account now with this link below.";
        body += "<br /><" + activationUrl;
        body += "<br />< Please note that you will be able to login using: ";
        body += "<br />< StudentID = " + IdTxt.Text;
        body += "<br />< Password = " + randomPw;
        body += "<br />< Thank you.";

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        };

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
    }

1 个答案:

答案 0 :(得分:0)

您是从gmail发送电子邮件吗?有一些小设置需要完成。 (我不记得确切的设置,如果你不这样做,你会得到一个错误,只是谷歌你会发现那是什么。)

我看到你使用String来构建你的消息,不要这样做,而是使用StringBuilder。

检查此代码段。

            MailMessage mailObj = new MailMessage();
            //verification mail
            mailObj.Subject = "Your Email Subject line";

            mailObj.Body =  ComposeVerficationMailBody(emailDetails.MailMessage, emailDetails.RecipientName);
           emailDetails.SenderName = "Your Website Name";


            mailObj.IsBodyHtml = true;
            mailObj.Priority = System.Net.Mail.MailPriority.High;

            //TO
            mailObj.To.Add(emailDetails.RecipientEmailAddress);
            mailObj.BodyEncoding = Encoding.Default;
            mailObj.ReplyToList.Add(emailDetails.ReplyToEmailAddress);
            //From
            mailObj.From = new MailAddress(senderEmailAddress, senderDisplayName);
            using (SmtpClient mailClient = new SmtpClient())
            {
                mailClient.Host = "gmail host";
                mailClient.Port = 587;
                mailClient.EnableSsl = false;
                mailClient.UseDefaultCredentials = true;
                mailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                //mailClient.Credentials = new NetworkCredential("senderEmail", "senderPassword");


                MailAddress msg = new MailAddress(emailDetails.ReplyToEmailAddress);
                mailClient.Timeout = 500000;
                mailClient.Send(mailObj);



 private String ComposeVerficationMailBody()
 {
    StringBuilder emailMessage = new StringBuilder();
   //build your message here
   return emailMessage.toString();
 }

希望这足以满足您的要求。