如何从.aspx页面发送电子邮件?

时间:2012-06-26 06:21:34

标签: asp.net

我在.aspx页面上使用了文本框和提交按钮,我想在按钮点击时通过电子邮件发送所有这些文本框的数据,所以请告诉我解决方案......

5 个答案:

答案 0 :(得分:3)

按钮单击事件上的

调用此函数

public bool SendOnlyToEmail(string sToMailAddr, string sSubject, string sMessage,
                                         string sFromMailAddr)
            {
                try
                {
                    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                    if (string.IsNullOrEmpty(sFromMailAddr))
                    {
// fetching from address from web config key
                        msg.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["MailFrom"]); 
                    }
                    else
                    {
                        msg.From = new System.Net.Mail.MailAddress(sFromMailAddr);
                    }

                    foreach (string address in sToMailAddr)
                    {
                        if (address.Length > 0)
                        {
                            msg.To.Add(address);
                        }
                    }
                    msg.Subject = sSubject;
                    msg.Body = sMessage;
                    msg.IsBodyHtml = true;

                    //fetching smtp address from web config key
                    System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings["MailServer"]);
                    //SmtpMail.SmtpServer = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MailServer"]);
                    if (sToMailAddr.Length > 0)
                    {
                        objSMTPClient.Send(msg);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception objException)
                {
                    ErrorLog.InsertException(objException);
                    return false;
                }
            }

答案 1 :(得分:2)

您可以使用SmtpClient类。

答案 2 :(得分:2)

没有解决此问题的纯代码方法;您依赖于使用SMTP服务器来发送邮件。最佳情况:您已在服务器上使用默认端口进行了一次设置。在这种情况下,您只需要:

SmtpClient client = new SmtpClient("localhost");
client.Send(new MailMessage("me@myserver.com", "someoneelse@foo.com"));

如果做不到这一点,您可以考虑设置一个免费的SMTP帐户,或者(如果您计划发送批量电子邮件,绝对必要),通过Amazon SES等电子邮件服务提供商获取帐户。

答案 3 :(得分:1)

您可以使用以下代码发送电子邮件:

SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("senderEmail");
message.From = fromAddress;
message.Subject = "your subject";
message.Body = txtBox.Text;//Here put the textbox text
message.To.Add("to");
smtpClient.Send(message);//returns the boolean value ie. success:true

答案 4 :(得分:1)

    <script runat="server">

        protected void Button1_Click(object sender, EventArgs e)
        {
            //create the mail message
            MailMessage mail = new MailMessage();

            //set the addresses
            mail.From = new MailAddress("info@infoA2Z.com");
            mail.To.Add("Support@infoA2Z.com");

            //set the content
            mail.Subject = "This is an email";
            mail.Body = "this is the body content of the email.";

            //send the message
            SmtpClient smtp = new SmtpClient();
            smtp.Send(mail);
        }
    </script>