C# how to send email?

后端 未结 5 1395
傲寒
傲寒 2020-12-03 16:20

I am using C#.NET 4.0 and would like to send an email to an address with a subject and a body, the body will contain some information from a few text-boxes in my application

相关标签:
5条回答
  • 2020-12-03 16:50

    Is smtp.server.com really an SMTP server? You need to replace that with a real one. Your ISP probably provides you one, but it will likely only relay for emails originating from an address that your ISP owns.

    0 讨论(0)
  • 2020-12-03 16:57

    I have worked with three well known ISP’s to host my client’s websites. All three ISP's instructed me to use “localhost” as the smtp server name.

    0 讨论(0)
  • 2020-12-03 17:00

    Probably your authentication (credentials) or servername/port is not correct.

    Try this:

            MailMessage mailMsg = new MailMessage();
            mailMsg.To.Add("test@hotmail.com");
                        // From
            MailAddress mailAddress = new MailAddress("you@hotmail.com");
            mailMsg.From = mailAddress;
    
            // Subject and Body
            mailMsg.Subject = "subject";
            mailMsg.Body = "body";
    
            // Init SmtpClient and send on port 587 in my case. (Usual=port25)
            SmtpClient smtpClient = new SmtpClient("mailserver", 587);
            System.Net.NetworkCredential credentials = 
               new System.Net.NetworkCredential("username", "password");
            smtpClient.Credentials = credentials;
    
            smtpClient.Send(mailMsg);
    
    0 讨论(0)
  • 2020-12-03 17:10

    you cannot leave this string:

    smtp.server.com

    you should have there the name of your smtp server, usually something like mail.yourcompanyname.com or smtp.yourcompanyname.com

    0 讨论(0)
  • 2020-12-03 17:12

    Add this:

    SmtpServer.EnableSsl = true;
    
    0 讨论(0)
提交回复
热议问题