未显示任何错误,但邮件未发送 [英] Not showing any error but mail not sending

查看:171
本文介绍了未显示任何错误,但邮件未发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Net;
public partial class forget : System.Web.UI.Page
{
    private object lblMessage;

    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            DataSet ds = new DataSet();
            String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                //SqlCommand cmd = new SqlCommand("SELECT UserName,Password FROM UserInfo Where Email= '" + txtEmail.Text.Trim() + "'", con);
                string Command = "select uname,pwd from register where email = @emailid and  Security_1 = @Security1 and Security_2 = @Security2";
                SqlCommand cmd = new SqlCommand(Command, con);
               
                cmd.Parameters.AddWithValue("@emailid", txt_email.Text.Trim());
                cmd.Parameters.AddWithValue("@Security1", txt_security1.Text);
                cmd.Parameters.AddWithValue("@Security2", txt_security2.Text);
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txt_email.Text);
                // Recipient e-mail address.
                Msg.To.Add(txt_email.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = "Hi, <br>Please check your Login Detailss<br><br>Your Username: " + ds.Tables[0].Rows[0]["uname"] + "<br><br>Your Password: " + ds.Tables[0].Rows[0]["pwd"] + "<br><br>";
                Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("tmjagadeesh.95@gmail.com", "password");
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                //Msg = null;
                lbltxt.Text = "Your Password Details Sent to your mail";
                // Clear the textbox valuess
                txt_email.Text= "";
            }
            else
            {
                lbltxt.Text = "The Email you entered not exists.";
            }
        }
        catch(Exception ex)
    {
            Console.WriteLine("{0} Exception caught.", ex);
    }

    }
}



我尝试过的事情:

我正在从忘记密码"页面中获取详细信息,然后按提交按钮邮件需要发送



What I have tried:

i am geting a detail from forget password page and press submit button mail need to send

推荐答案

尝试一下-
为我工作

try this one -
working for me

using System.Net.Mail;
using System.Text;

...

// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user@gmail.com","password");

MailMessage mm = new MailMessage("mymail@domain.com", "sender@domain.com", "test", "test");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

client.Send(mm);


这篇关于未显示任何错误,但邮件未发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆