在asp.net发送邮件
我使用的是asp.net 3.5和C#。
我想从asp.net发送邮件,因为我从我的托管服务提供商那里得到了一些细节
这些是:
- mail.MySite.net
- 用户名
- 密码
但是我无法通过这些详细信息发送邮件,我在web.config文件中进行了以下更改:
另外,在后面的代码我写这个函数:
MailMessage mail = new MailMessage("webmaster@mySite.net", "XYZ@gmail.com"); mail.Subject = "Hi"; mail.Body = "Test Mail from ASP.NET"; mail.IsBodyHtml = false; SmtpClient smp = new SmtpClient(); smp.Send(mail);
但由于消息发送失败,我收到错误消息。
请让我知道我做错了什么以及我必须做些什么才能让它正常工作。
提前致谢。
您需要提供客户端凭据吗?
smp.Credentials = CredentialCache.DefaultNetworkCredentials;
要么
smp.Credentials = new NetworkCredential("yourUserID", "yourPassword", "yourDomainName");
此外,您获得的确切exception将是有用的。
请参阅Scott Guthrie的post以获取更多帮助。
我怀疑端口8080是正确的smtp端口。 也许是25号或587号港口。
通过asp.net c#发送电子邮件并不是一件复杂的事情……我们只知道smtp端口和主机…
MailAddress to = new MailAddress("Email Id"); MailAddress from = new MailAddress("Email Id"); MailMessage mail = new MailMessage(from, to); mail.Subject = ""; mail.Body = ""; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.Credentials = new NetworkCredential( "Email Id", "Password"); smtp.EnableSsl = true; smtp.Send(mail);
不使用SMTP,使用Microsoft.Office.Interop.Outlook添加; 参考
Application app = new Application(); NameSpace ns = app.GetNamespace("mapi"); ns.Logon("Email-Id", "Password", false, true); MailItem message = (MailItem)app.CreateItem(OlItemType.olMailItem); message.To = "To-Email_ID"; message.Subject = "A simple test message"; message.Body = "This is a test. It should work"; message.Attachments.Add(@"File_Path", Type.Missing, Type.Missing, Type.Missing); message.Send(); ns.Logoff();
我的代码与您的代码非常相似,我认为区别在于您需要在SMTP客户端的构造函数中为SMTP服务器提供IP地址。
MailMessage Email = new MailMessage("donotreply@test.com", "receiver@test.com"); Email.Subject = "RE: Hello World."; Email.Body = "Hello World"; Email.IsBodyHtml = false; SmtpClient Client = new SmtpClient(SMTP_SERVER); //This will be an IP address Client.Send(Email);
希望有所帮助! 🙂
(顺便说一句,我在Winforms,Windows服务和ASP .NET中使用过它。在ASP .NET中我不需要在aspx页面中提供任何东西。)