使用ASP.NET和C#发送邮件/联系表单 [英] Send Mail / Contact Form using ASP.NET and C#

查看:52
本文介绍了使用ASP.NET和C#发送邮件/联系表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我用过这个tutorlal Send Mail /使用ASP.NET和C#联系表单 [ ^ ]但是我收到一个错误:



指定的字符串不是电子邮件地址所需的格式。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.FormatException:指定的字符串不是电子邮件地址所需的格式。

Hi
I have used this tutorlal Send Mail / Contact Form using ASP.NET and C#[^] but I get a error:

The specified string is not in the form required for an e-mail address.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

Source Error: 

Line 41:         }
Line 42:         // Passing values to smtp object
Line 43:         smtp.Send(fromAddress, toAddress, subject, body);
Line 44:     }
Line 45:     protected void Button_send_Click(object sender, EventArgs e)



这是我的后端代码的样子,是什么我做错了吗?


This is how my backend code looks like, what have I been doing wrong?

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class kontakt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "myEmail";
        // any address where the email will be sending
        var toAddress = TextBox_navn.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "password";
        // Passing the values and make a email formate to display
        string subject = TextBox_emne.Text.ToString();
        string body = "From: " + TextBox_navn.Text + "\n";
        body += "Email: " + TextBox_email.Text + "\n";
        body += "Subject: " + TextBox_emne.Text + "\n";
        body += "Question: \n" + TextBox_besked.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 Button_send_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "INSERT INTO kontakt (navn, emne, email, besked) VALUES(@navn, @emne, @email, @besked)";

        cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
        cmd.Parameters.Add("@emne", SqlDbType.Text).Value = TextBox_emne.Text;
        cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = TextBox_email.Text;
        cmd.Parameters.Add("@besked", SqlDbType.Text).Value = TextBox_besked.Text;



        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        SendMail();
        TextBox_navn.Text = "";
        TextBox_emne.Text = "";
        TextBox_email.Text = "";
        TextBox_besked.Text = "";
        Label1.Text = "Din besked er sendt";


          
    }
}





希望有人能帮助我用这个



Tina



Hope someone could help me with this

Tina

推荐答案

对于SMTP,声明消息的以下格式对我来说运行正常:

For SMTP, the following format to declare the message works fine for me:
MailMessage mail = new MailMessage();
// add message 'from' field
mail.From = new MailAddress("tinaw25@gmail.com");
// add recipient 'to' field
mail.To.Add("some@address.com");
// append the message subject
mail.Subject = "my mail subject";
// append the message body
mail.Body = "my message body";

smtp.Send(mail);


您好,首先您在asp.net页面中使用过代码,但无论何时您参考,它都使用控件而不是asp.net页面



您的参考代码工作正常..我看看它..



谢谢



快乐编码......
Hi, First of all you used code in asp.net page but whenever you take reference,it used control not asp.net page

your reference code are working fine..i check it out..

Thanks

Happy coding...


嗨...

看到这个链接,可能对你有用。



如何通过Asp跟踪电子邮件发送或不发送.Net? [ ^ ]



谢谢你。
Hi...
see this link,may its useful to u.

How I can track Email send or Not Through Asp .Net?[^]

thank u.


这篇关于使用ASP.NET和C#发送邮件/联系表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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