gpt4 book ai didi

asp.net - 使用 System.Web.Mail 发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 11:46:45 25 4
gpt4 key购买 nike

我想在 asp 中发送电子邮件。

我用这个代码

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);

但我得到错误:

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发送邮件?

最佳答案

我使用此代码。

 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);

关于asp.net - 使用 System.Web.Mail 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14662472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com