简体   繁体   English

在asp.net中使用smtp和gmail提交表单后发送电子邮件

[英]Sending an email once a form is submitted using smtp and gmail in asp.net

I'm trying to receive an email once my form is submitted on my webpage. 我的表单在我的网页上提交后,我正在尝试收到电子邮件。 At the moment it submits fine without any errors but I don't receive the email. 目前它提交罚款没有任何错误,但我没有收到电子邮件。 Does anyone know what code I have to add in the code behind page to make this work? 有谁知道我必须在代码隐藏页面中添加哪些代码才能使其工作?

Here is the html; 这是html;

<h2>Contact Us</h2>
        <br />
        <table>            
            <tr>
                <td  style="align-items:center">
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtName"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td  style="align-items:center">
                    email:</td>
                <td>
                    <asp:TextBox ID="txtEmail"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>



            <!-- Message -->
            <tr>
                <td style="align-items:center">
                    What are you looking for?
                </td>
                <td>
                    <asp:TextBox ID="txtMessage"
                                    runat="server"
                                    Columns="40"
                                    Rows="6"
                                    TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
             <tr>
                <td  style="align-items:center">
                    What would you be willing to pay for this app?</td>
                <td>
                    <asp:TextBox ID="txtPay"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>

            <!-- Submit -->
            <tr style="align-items:center">
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                        onclick="btnSubmit_Click" /><br />
                </td>
            </tr>

            <!-- Results -->
            <tr style="align-items:center">
                <td colspan="2">
                    <asp:Label ID="lblResult" runat="server"></asp:Label>
                </td>
            </tr>
        </table>

this is the code behind; 这是背后的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Telluswhatyouwant : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("ronan.byrne@mhlabs.net");

//Send the msg
            client.Send(msg);

This is a perfectly working code for localhost where mailing option is enabled. 这是localhost的完美工作代码,其中启用了邮件选项。 You can change the port number.fromWho, toWho are mailing addresses in string format (ie: david@yahoo.com) 您可以更改端口号。从Who,到WW是字符串格式的邮寄地址(即:david@yahoo.com)

    string sMailServer = "127.0.0.1";

    MailMessage MyMail = new MailMessage();
    MyMail.From = fromWho;
    MyMail.To = toWho;
    if (toCC != "" || toCC != null)
    {
        MyMail.Cc = toCC;
    }
    if (toBCC != "" || toBCC != null)
    {
        MyMail.Bcc = toBCC;
    }

    MyMail.Subject = Subject;
    MyMail.Body = Body;
    //MyMail.BodyEncoding = Encoding.UTF8;
    MyMail.BodyFormat = MailFormat.Html;

    SmtpMail.SmtpServer = sMailServer;
    try
    {

        SmtpMail.Send(MyMail);


    }
    catch (Exception ex)
    {
        return ex;
    }

You can try this and make sure you are using valid login credential and you have proper internet connection: 您可以尝试这一点,并确保您使用有效的登录凭证,并且您有正确的互联网连接:

 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = 
                  new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
     smtp.Send(mail);                
  }
  catch (Exception ex)
  {   
    // Handle exception here 

  }

Where you have configured your client Object. 您已配置client对象的位置。 Give complete detail. 提供完整的细节。

Then go through the following post answered by me. 然后通过我回答的以下帖子。 You should configure your SmtpClient properly. 您应该正确配置您的SmtpClient

How to send email in ASP.NET C# 如何在ASP.NET C#中发送电子邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM