简体   繁体   中英

Can we send mails from localhost using asp.net and c#?

i am using using System.Net.Mail;

and following code to send mail

MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient();

        // Set the sender's address
        message.From = new MailAddress("fromAddress");

     // Allow multiple "To" addresses to be separated by a semi-colon
        if (toAddress.Trim().Length > 0)
        {
            foreach (string addr in toAddress.Split(';'))
            {
                message.To.Add(new MailAddress(addr));
            }
        }
      // Allow multiple "Cc" addresses to be separated by a semi-colon
        if (ccAddress.Trim().Length > 0)
        {
            foreach (string addr in ccAddress.Split(';'))
            {
                message.CC.Add(new MailAddress(addr));
            }
        }
        // Set the subject and message body text
        message.Subject = subject;
        message.Body = messageBody;

        // Set the SMTP server to be used to send the message
        client.Host = "YourMailServer";

        // Send the e-mail message
        client.Send(message);

for Host i am providing client.Host = "localhost";

for this its falling with error

No connection could be made because the target machine actively refused it some_ip_address_here

and when i use client.Host = "smtp.gmail.com";

i get following error

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

i am not able to send mail through localhost. Please help me out, i am new to c# please correct me in code where i am going wrong..?

Here is some code that works for sending mail via gmail (code from somewhere here on stackoverflow. It is similar to the code here: Gmail: How to send an email programmatically ):

using (var client = new SmtpClient("smtp.gmail.com", 587)
{
  Credentials = new NetworkCredential("yourmail@gmail.com", "yourpassword"),
  EnableSsl = true
})
{
  client.Send("frommail@gmail.com", "tomail@gmail.com", "subject", message);
}

For sending mail from client.Host = "localhost" you need set up local SMTP server.

For sending mail via Google (or via any other SMTP server, including your own local SMTP) you must set username, password, ssl settings - all as required by SMTP server chosen, and you need to read their help for this.

For example Google says that you need SSL, port 465 or 587, server smtp.gmail.com and your username and password.

You can assign all this values in .config file.

<system.net>
  <mailSettings>
    <smtp>
      <network host="smtp.gmail.com" enableSsl="true" port="587" userName="yourname@gmail.com" password="password" />
    </smtp>
  </mailSettings>
</system.net>

Or set to SmtpClient in code before every use:

client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSSL = true;
client.Credentials = new NetworkCredential("yourname@gmail.com", "password");

Place this code inside a <configuration> </configuration> in web.config file

<system.net>
<mailSettings>
  <smtp>
    <network host="smtp.gmail.com" enableSsl="true" port="587" userName="youremail@gmail.com" password="yourpassword" />
  </smtp>
</mailSettings>
</system.net>

then backend code

MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("email@gmail.com");
message.To.Add(new MailAddress(TextBoxEadd.Text));
message.CC.Add(new MailAddress("email@gmail.com"));
message.Subject = "New User Registration ! ";
message.Body =  "HELLO";
sr.Close();
SmtpClient client = new SmtpClient();
client.Send(message);

I hope this code help you! :)

use this Line..Dont Use Port And HostName

LocalClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

I just wanted to add that Gmail now requires App Password in order to use it from other applications. Check this link . I had to find it the hard way. After I created an App Password and then I changed the NetworkCredentials to use it to send emails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM