Send an email to asp.net

I am using asp.net 3.5 and c #.

I want to send mail from asp.net because I have some information from my hosting provider

which are as follows:

  • mail.MySite.net
  • Username
  • Password

But I can not send mail through this data, I made 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, in 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 get an error when the message fails.

Please let me know what I am doing wrong and what I have to do to make it work properly.

Thanks in advance.

+3
source share
5 answers

Need to provide customer credentials?

smp.Credentials = CredentialCache.DefaultNetworkCredentials;

or

smp.Credentials = new NetworkCredential("yourUserID", "yourPassword", "yourDomainName");

, , .

. .

+2

, 8080 smtp. , 25 587.

+2

asp.net # ... 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);
+1

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

, , , , IP- SMTP- SMTP-.

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

, !:)

(Btw, Winforms, Windows ASP.NET. ASP.NET aspx.)

0

Source: https://habr.com/ru/post/1747264/


All Articles