可以使用C#中的asp.net项目从我的电脑(localhost)发送电子邮件吗?
最后我要将我的项目上传到webserver,但我想在上传之前进行测试.
最后我要将我的项目上传到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);