Send mail in asp.net

不问归期 提交于 2019-12-23 17:00:13

问题


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 through 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.


回答1:


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.




回答2:


I doubt port 8080 is the correct smtp port. Perhaps port 25 or 587.




回答3:


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



回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/2916841/send-mail-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!