Send mail in asp.net
I am using asp.net 3.5 and C#.
I want to send mail from asp.net, for that I have got some details from my hosting provider
which are these:
- mail.MySite.net
- UserName
- Password
But I am unable to send mail throug开发者_StackOverflowh these details, I have done the following changes in my web.config file:
<system.net>
<mailSettings>
<smtp>
<network
host="mail.MySite.net"
port="8080"
userName="UserName"
password="Password" />
</smtp>
</mailSettings>
</system.net>
Also, at the code behind I am writing this function:
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);
but I am getting error message as message sending failed.
Please let me know what I am doing wrong and what I have to do to make it work fine.
Thanks in advance.
Do you need to provide the client credentials?
smp.Credentials = CredentialCache.DefaultNetworkCredentials;
or
smp.Credentials = new NetworkCredential("yourUserID", "yourPassword", "yourDomainName");
Also, the exact exception you are getting would be useful.
See a post by Scott Guthrie for more help.
I doubt port 8080 is the correct smtp port. Perhaps port 25 or 587.
Sending an email through asp.net c# is not a complicated thing... just we know about smtp port and host...
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);
Without using SMTP,Add using Microsoft.Office.Interop.Outlook; reference
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();
I have very similar code to yours that works, I think the difference is you need to supply the IP address to your SMTP server in the constructor for the SMTP client.
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);
Hope that helps! :)
(Btw, I've used this in Winforms, windows services, and ASP .NET. In ASP .NET I didn't need to supply anything in the aspx page.)
精彩评论