通过asp.net发送邮件 [英] Sending Mail by asp.net

查看:136
本文介绍了通过asp.net发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个注册页面,我希望任何用户注册的邮件都应该出现在我的电子邮件地址上。我在WiFi网络上。所以请帮帮我。



我的.cs代码如下:





I have made a registration page and I want that any user registers than a mail should come on my email address. I am on a WiFi network. So please please help me out.

my .cs code is as follows:


protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         try
        {
            string str ="Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Earth;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string insertQuery = "insert into Corporate (Name,Emailaddress,Contact,Highestqualification,Specialization,Institutename,Description) values (@Name,@Emailaddress,@Contact,@Highestqualification,@Specialization,@Institutename,@Description)";
            SqlCommand com = new SqlCommand(insertQuery, con);
            com.Parameters.AddWithValue("@Name", txtName.Text);
            com.Parameters.AddWithValue("@Emailaddress", txtEmailaddress.Text);
            com.Parameters.AddWithValue("@Contact", txtContactno.Text);
            com.Parameters.AddWithValue("@Highestqualification", txtHighestqualification.Text);
            com.Parameters.AddWithValue("@Specialization", ddlspecialization.SelectedItem.ToString());
            com.Parameters.AddWithValue("@Institutename", txtInstitutename.Text);
            com.Parameters.AddWithValue("@Description", txtDescription.Text);
            

            com.ExecuteNonQuery();
           
            

            con.Close();
            
        }

        catch (Exception ex)
        {
            Response.Write("Error Occured! Try Again" + ex.ToString());
        }

        
        Response.Write("Thanks for Applying, We will response on your application shortly");
    }
}

推荐答案

protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "ml27.santos@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString();
    //Password of your gmail address
    const string fromPassword = "Password";
    // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //here on button click what will done
        SendMail();
        DisplayMessage.Text = "Message sent!";
        DisplayMessage.Visible = true;
        YourSubject.Text = "";
        YourEmail.Text = "";
        YourName.Text = "";
        Comments.Text = "";
    }
    catch (Exception) { }
}}


string res ="";

       
        try
        {
           
                string mailid = string.Empty;
                MailMessage msg = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                mailid = emailid.ToString();
                msg.From = new MailAddress("yr_adress");
                msg.To.Add(mailid);
               

                msg.Subject = msgSubject.ToString();
                msg.Body = msgbody.ToString();
                msg.IsBodyHtml = true;
                msg.Priority = MailPriority.Normal;
                smtp.Credentials = new NetworkCredential("youremailid ", "Password"); // send  form yr email address..
                smtp.Port = 587;
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                try
                {
                    smtp.Send(msg);
                    res = "Sucsessfully send";
                }
                catch (SmtpFailedRecipientsException ex)
                {
                    for (int i = 0; i < ex.InnerExceptions.Length; i++)
                    {
                        SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                        if (status == SmtpStatusCode.MailboxBusy ||status == SmtpStatusCode.MailboxUnavailable)
                        {
                            Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                            System.Threading.Thread.Sleep(5000);
                            smtp.Send(msg);
                            res = "Sucsessfully send";
                        }
                        else
                        {
                            Console.WriteLine("Failed to deliver message to {0}",
                                ex.InnerExceptions[i].FailedRecipient);
                            res = "send Faild";
                        }
                    }
                }
            }
        }
        
            
            catch (Exception ex)
            {
             Console.WriteLine("Exception caught in RetryIfBusy(): {0}", ex.Message.ToString());
             res = "send Faild";
            }

        return res;
    }


这篇关于通过asp.net发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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