如何在ASP.NET中发送电子邮件? [英] How can I send email in ASP.NET?

查看:96
本文介绍了如何在ASP.NET中发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.NET3.5中发送电子邮件.

我尝试了这段代码,但没有用.

How can I send email in ASP.NET3.5.

I tried this code but it isn''t working.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;
using System.Web;

public partial class Smtp_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label9.Text = "";
    }

     protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = TextBox1.Text;
            mailMessage.To = TextBox2.Text;
            mailMessage.Cc = TextBox3.Text;
            mailMessage.Bcc = TextBox4.Text;
            mailMessage.Subject = TextBox5.Text;
            mailMessage.Body = TextBox6.Text;
           // string attach1 = null;
            
     		
            //string strFileName = null;
            /* Set the SMTP server and send the email with attachment */
            // SmtpMail.SmtpServer = "127.0.0.1";
            SmtpMail.SmtpServer.Insert(0, "smtp.yahoo.com");
            SmtpMail.Send(mailMessage);
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            
            
            Label9.Visible = true;
            
            Label9.Text = "Message sent.";
        }
        catch (Exception ex)
        {
            Label9.Visible = true;
            Label9.Text = ex.ToString();
        }
    }
}

<pre><pre lang="cs">using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;
using System.Web;


public partial class Smtp_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label9.Text = "";
    }

    protected void InitializeComponent()
    {
        this.Button1.Click += new System.EventHandler(this.Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }

     protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            MailMessage mailMessage = new MailMessage();

            mailMessage.From = TextBox1.Text;
            mailMessage.To = TextBox2.Text;
            mailMessage.Cc = TextBox3.Text;
            mailMessage.Bcc = TextBox4.Text;
            mailMessage.Subject = TextBox5.Text;
            mailMessage.Body = TextBox6.Text;

           // string attach1 = null;



            //string strFileName = null;

            /* Set the SMTP server and send the email with attachment */

            // SmtpMail.SmtpServer = "127.0.0.1";
            SmtpMail.SmtpServer.Insert(0, "smtp.yahoo.com");

            SmtpMail.Send(mailMessage);

            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";


            Label9.Visible = true;

            Label9.Text = "Message sent.";
        }
        catch (Exception ex)
        {
            Label9.Visible = true;
            Label9.Text = ex.ToString();
        }
    }
}


推荐答案

可能是由于各种原因.您需要一一看待它们.
港口开放吗?防火墙权限到位了吗?
进一步确保已在Web.Config中配置SMTP配置:
It can be because of various reasons. You need to look at them one by one.
Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>


如果需要,请查看此Microsoft Video教程:
使用ASP.NET发送来自网站的电子邮件 [


If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]


如果您使用"smtp.yahoo.com"服务,则必须分配端口不,您的Yahoo地址和SmtpClient对象上的密码.

或者,您也可以通过您的gmail帐户发送邮件.

使用ASP.net通过Gmail帐户(Gmail SMTP服务器帐户)发送电子邮件 [ ^ ]
If you use "smtp.yahoo.com" service you''ll have to assign the Port No, your yahoo address and the password on you SmtpClient object.

Or you also can send mail through your gmail account.

Sending E-mail using ASP.net through Gmail account (Gmail SMTP Server account)[^]


这篇关于如何在ASP.NET中发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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