加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

发送电子邮件asp.net c#

发布时间:2020-12-15 23:05:01 所属栏目:asp.Net 来源:网络整理
导读:可以使用C#中的asp.net项目从我的电脑(localhost)发送电子邮件吗? 最后我要将我的项目上传到webserver,但我想在上传之前进行测试. 我已经找到了准备好的源代码,并尝试在本地主机中运行它们,但是它们都不成功. 例如这个代码: using System; using System.Co
可以使用C#中的asp.net项目从我的电脑(localhost)发送电子邮件吗?
最后我要将我的项目上传到webserver,但我想在上传之前进行测试.

我已经找到了准备好的源代码,并尝试在本地主机中运行它们,但是它们都不成功.
例如这个代码:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net.Mail;

    namespace sendEmail
    {
        public partial class _default : System.Web.UI.Page
        {
            protected void Page_Load(object sender,EventArgs e)
            {

            }
            protected void Btn_SendMail_Click(object sender,EventArgs e)
            {
                MailMessage mailObj = new MailMessage(
                    txtFrom.Text,txtTo.Text,txtSubject.Text,txtBody.Text);
                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.ToString();
                }
            }
        }
    }

那么如何使用asp.net C#发送电子邮件?我应该设置一些服务器配置吗?

解决方法

从Asp.Net发送电子邮件:
MailMessage objMail = new MailMessage("Sending From","Sending To","Email Subject","Email Body");
    NetworkCredential objNC = new NetworkCredential("Sender Email","Sender Password");
    SmtpClient objsmtp = new SmtpClient("smtp.live.com",587); // for hotmail
    objsmtp.EnableSsl = true;
    objsmtp.Credentials = objNC;
    objsmtp.Send(objMail);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读