-10

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

4

5 回答 5

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;
                }
            }
于 2012-06-26T06:33:10.800 回答
2

你可以使用SmtpClient类。

于 2012-06-26T06:23:00.830 回答
2

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

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

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

于 2012-06-26T06:28:48.080 回答
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
于 2012-06-26T06:28:50.507 回答
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>
于 2012-12-16T20:24:24.450 回答