Iva*_*van 2 c# web-config smtpclient asp.net-4.0
我有一个网络表单,有人可以设置一个帐户 - 我想给他们发送电子邮件确认.
我正在使用的代码:
// Create e-mail message
MailMessage mm = new MailMessage();
mm.From = new MailAddress("OurOrganisationEmail@ourdomain.com", "Our Organisation");
mm.To.Add(new MailAddress("webuser@domain.com", "Web User"));
mm.Subject = "Confirmation";
mm.Body = "You are now registered test";
mm.IsBodyHtml = true;
// Send e-mail
sc = new SmtpClient();
NetworkCredential basicAuthenticationInfo = new NetworkCredential(“OurOrganisationEmail@ourdomain.com”, “ourorganisationemailPassword”);
sc.Credentials = basicAuthenticationInfo;
sc.UseDefaultCredentials = false;
sc.Send(mm);
Run Code Online (Sandbox Code Playgroud)
web.config中:
<system.net>
<mailSettings>
<smtp>
<network host="mail.OurOrganisationEmail@ourdomain.com" port="9999" userName="OurOrganisationEmail@ourdomain.com" password="OurOrganisationEmailPassword"/>
</smtp>
</mailSettings>
</system.net>
Run Code Online (Sandbox Code Playgroud)
但得到:
System.Net.Mail.SmtpFailedRecipientException was caught
HResult=-2146233088
Message=Mailbox unavailable. The server response was: Authentication is required for relay
Source=System
FailedRecipient=<webuser@domain.com>
Run Code Online (Sandbox Code Playgroud)
看起来它想要我想要发送到的网址的用户登录详细信息.我该如何修改并发送所有其他商业公司的确认电子邮件?
好的 - 搞定了.以下是工作编码; 看起来我配置NetworkCredential对象是个问题.感谢大家帮助我找到解决方案.
MailAddress fromAddress = new MailAddress("OurOrganisationEmail@ourdomain.com","Our Organisation");//"name@yourdomain.com";
MailAddress toAddress = new MailAddress("webuser@domain.com", "Web User"); //"name@anydomain.com";
//Create the MailMessage instance
MailMessage myMailMessage = new MailMessage(fromAddress, toAddress);
//Assign the MailMessage's properties
myMailMessage.Subject = "Confirmation";
myMailMessage.Body = "You are now registered test";
myMailMessage.IsBodyHtml = true;
//Create the SmtpClient object
SmtpClient smtp = new SmtpClient();
//Send the MailMessage (will use the Web.config settings)
smtp.Send(myMailMessage);
Run Code Online (Sandbox Code Playgroud)
web.config中
<system.net>
<mailSettings>
<smtp>
<network host="mail.ourdomain.com" port="9999" userName="OurOrganisationEmail@ourdomain.com" password="OurOrganisationEmailPassword"/>
</smtp>
</mailSettings>
</system.net>
Run Code Online (Sandbox Code Playgroud)