发送电​​子邮件使用System.Web.Mail [英] Send email with System.Web.Mail

查看:164
本文介绍了发送电​​子邮件使用System.Web.Mail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ASP发送电子邮件。

I want send email in asp.

我用这个code

using System.Web.Mail;

MailMessage msg = new MailMessage();
msg.To = "aspnet@yahoo.com";
msg.From = "info@mysite.com";
msg.Subject = "Send mail sample";
msg.BodyFormat = MailFormat.Html;
string msgBody="Hello My Friend. This is a test.";
msg.Body = msgBody ;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(msg);

但我得到的错误:

But i get error :

命令的次序错误。服务器响应为:尝试发送到非本地电子邮件地址时,该邮件服务器需要身份验证。请检查您的邮件客户端设置或与管理员联系以验证该域或地址为该服务器定义的。

Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

如何使用ASP发送电子邮件?

How to send email with asp?

推荐答案

我用这个code。

 MailMessage msg = new MailMessage();
 msg.Body = "Body";

 string smtpServer = "mail.DomainName";
 string userName = "info@mysite.com";
 string password = "MyPassword";
 int cdoBasic = 1;
 int cdoSendUsingPort = 2;
 if (userName.Length > 0)
  {
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
    }
    msg.To = user.Email;
    msg.From = "info@Mysite.com";
    msg.Subject = "Subject";
    msg.BodyEncoding = System.Text.Encoding.UTF8;
    SmtpMail.SmtpServer = smtpServer;
   SmtpMail.Send(msg);

这篇关于发送电​​子邮件使用System.Web.Mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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