如何设置asp.net web表单应用程序在Windows 7上发送smtp电子邮件,以便我可以测试?

时间:2015-08-09 00:16:30

标签: asp.net smtp system.net.mail

我试图在Windows 7上的asp.net web表单应用程序中通过smtp测试发送电子邮件。我得到超时。我正在使用我的快速帐户进行测试。我很困惑,因为我不确定我是否还需要通过我的iis控制台管理器设置smtp电子邮件。这是我在控制台管理器中首先进行的设置。我很困惑这个过程是如何工作的。 asp.net应用程序首先发送到smtp电子邮件然后发送到fastmail smtp吗?

smtp电子邮件页面

     e-mail address: "is set to my fastmail address"
     deliver email to smtp server is checked.
     smtp server: mail.messagingengine.com   
     use localhost is unchecked
     port: 25   fastmail port is 465 but was told to set this to 25 
     authentication settings: i have this set to my email address and password   

这是会话状态页面

    session state mode settings: in process
    cookie settings
    mode: use cookies
   name: asp.net_sessionid
   use hosting identity for impoersonation is checked

接下来是我在asp.net应用程序中的web.config文件

    <system.net>
      <mailsettings>
        <smtp deliveryMethod="Network" from"roger &lt;myname@fastmail.us&gt;">
          <network host="mail.messagingengine.com" userName="myname@fastmail.us" password="myemailpassword" port="465" enableSsl="true"  />
      </smtp>
     </mailSettings>
    </system.net>

接下来是我的代码

        protected void btn_send_Click(object sender, EventArgs e)
{
    try { 
    string sendername = txtbx_name.Text.ToString();
    MailMessage mymessage = new MailMessage();
    mymessage.Subject = txtbx_subject.Text.ToString();
    mymessage.Body = txtbx_message.Text.ToString();
    mymessage.From = new MailAddress("george@georgebush.me", sendername);
    mymessage.To.Add(new MailAddress("myemail address", "Roger"));        

    SmtpClient mysmtpclient = new SmtpClient();
    mysmtpclient.UseDefaultCredentials = false;
    mysmtpclient.DeliveryMethod   = SmtpDeliveryMethod.Network;

        mysmtpclient.Send(mymessage);            
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
    }
    catch (Exception ex)
    {
        string message = ex.ToString();
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);

    }
}

我假设这里不需要html和css。任何帮助表示赞赏。我甚至无法捕获try catch语句中的错误。我很遗憾,因为我是asp.net的新手。

1 个答案:

答案 0 :(得分:0)

我猜您的代码是正确的,您必须为:

添加地址
 mymessage.To.Add(new MailAddress("myemail address", "Roger"));  

而不是“mymail地址”只是放了一个电子邮件地址

你检查了你的设置文件,你忘记了一个相同的标记,如下所示:

<smtp deliveryMethod="Network" from"roger &lt;myname@fastmail.us&gt;">