发送邮件表单异常:System.Runtime.InteropServices.COMException:??? ?????? “SendUsing”???? ?÷?

时间:2012-01-24 17:59:59

标签: c# asp.net

我试图在我的网站上创建一个电子邮件表单(现在是一个本地主机), 当我运行我的代码,并按下“发送”时,它抛出了我的异常:

  

System.Runtime.InteropServices.COMException:??? ?????? “SendUsing”   ???? ?÷?

可能是什么问题?

这是我的代码:

<%@ Import Namespace="System.Web.Mail" %>

<script language="c#" runat="server">
    protected void Button2_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();

        msg.To = "XXX@gmail.com";

        msg.From = EmailTxt.Text;

        msg.Subject = "Fun With Daniel Email: from" + UsernameTxt.Text;

        msg.Body = MessegeBox.Text;
        msg.BodyFormat = MailFormat.Text;
        ErrorMessege.Text = "Sending..";


        System.Web.Mail.SmtpMail.Send(msg);

        ErrorMessege.Text = "The Email Sent successfully!";



    }

</script>

<td>
                                <asp:TextBox ID="UsernameTxt" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ErrorMessage="Please insert a Username"
                                    ControlToValidate="UsernameTxt" ForeColor="Red" Display="None" 
                                    SetFocusOnError="True"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Email
                            </td>
                            <td>
                                <asp:TextBox ID="EmailTxt" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="Please insert Password"
                                    ControlToValidate="EmailTxt" ForeColor="Red" Display="None" 
                                    SetFocusOnError="True"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Messege
                            </td>
                            <td>
                                <asp:TextBox ID="MessegeBox" runat="server" Height="100px" TextMode="MultiLine"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="You have to write a messege"
                                    ControlToValidate="MessegeBox" ForeColor="Red" Display="None" 
                                    SetFocusOnError="True"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="Button2" runat="server" Text="Login" OnClick="Button2_Click" />

                            </td>
                            <td>

                                <asp:Label ID="ErrorMessege" runat="server" ForeColor="Red"></asp:Label>
                                <asp:ValidationSummary ID="ValidationSummary2" runat="server" ForeColor="Red" />
                            </td>

                        </tr>

2 个答案:

答案 0 :(得分:4)

System.Web.Mail需要CDONT和CDOSYS,因此在这种情况下您必须首先考虑这一点。有关常见问题的解决方案,请参见this网站。

整个网站专门针对此命名空间问题的事实应该告诉您最好使用System.Net.Mail,这是一个原生SMTP实现,并且不依赖于COM组件。除非您使用.NET 1.x,否则应尽可能切换到它。

答案 1 :(得分:3)

您在尝试发送之前是否设置了SmtpServer名称?

它会设置为 -

System.Web.Mail.SmtpMail.SmtpServer = "your mail server name goes here";

并且应该在System.Web.Mail.SmtpMail.Send(msg);

之前

可以在MSDN

上找到有关如何使用SmtpMail的示例