Theun Arbeider
Theun Arbeider

Reputation: 5409

Sending email with ASP.net

So i've been searching stackoverflow for a way to send emails using a gmail account via a asp website...

I've tried many ways including Sending email in .NET through Gmail which seemed to be the best due to amount of upvotes he got.

However sadly it still doesn't work for me! I keep getting a time out.

Here's my code:

 var fromaddress = new MailAddress("[email protected]", "from");
 var toaddress = new MailAddress("[email protected]", "to");
 try
 {
     using (var smtpClient = new SmtpClient())
     {              
         smtpClient.EnableSsl = true;
         using (var message = new MailMessage(fromaddress, toaddress))
         {
             message.Subject = "Test";
             message.Body = "Testing this shit!";
             smtpClient.Send(message);
             return true;
         }
     }
 }
 catch (Exception ex)
 {
     return false;
 }

in my web.config I have

  <system.net>
    <mailSettings>
      <smtp from="[email protected]" deliveryMethod="Network">
        <network userName="[email protected]" password="mypassword" host="smtp.gmail.com" port="587"/>
      </smtp>             
    </mailSettings>
  </system.net>

According to several sites i've visited this should work!!! .. but it doesn't.

Is there still anything i'm doing wrong?

Upvotes: 2

Views: 2868

Answers (6)

Niranjan Singh
Niranjan Singh

Reputation: 18290

Put these settings EnableSSL = true and defaultCredentials="false" in your web.config settings. Gmail smtp server requires SSL set to true and mailclient.UseDefaultCredentials = false should be false if you are providing your credentials.

Update Web.Config Settings:

<mailSettings>
  <smtp from="[email protected]" deliveryMethod="Network">
    <network userName="[email protected]" password="mypassword" host="smtp.gmail.com" defaultCredentials="false" port="587" enableSsl="true"/>
  </smtp>             
</mailSettings>

And check this shorter code to send mail after providing settings in the web.config. even it send email much fast rather then specifying setting while creating the smtpclient setting in the mail sending function.

Public void SendEmail(string toEmailAddress, string mailBody)
        {
            try
            {
                MailMessage mailMessage = new System.Net.Mail.MailMessage();
                mailMessage.To.Add(toEmailAddress);
                mailMessage.Subject = "Mail Subjectxxxx";
                mailMessage.Body = mailBody;
                var smtpClient = new SmtpClient();
                smtpClient.Send(mailMessage);
                return "Mail send successfully";
            }
            catch (SmtpException ex)
            {
                return "Mail send failed:" + ex.Message;
            }

Working very much fine at my side..

Upvotes: 0

Pleun
Pleun

Reputation: 8920

Your code seems fine to me.

Try to deliberately enter false credentials. If you get a different errormessage you are connected to gmail and there is a problem there.

If you get the same timeout problem, it is probably not a software thing but a firewall issue.

longshot - update Perhaps there is a web.config issue? Try to specify everything in code like this. I have this working in real life with Gmail so if this does not work it definitely is a firewall/connection thing.

           SmtpClient mailClient = new SmtpClient();
            //This object stores the authentication values     
            System.Net.NetworkCredential basicCredential =
                new System.Net.NetworkCredential("[email protected]", "****");
            mailClient.Host = "smtp.gmail.com";
            mailClient.Port = 587;
            mailClient.EnableSsl = true;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = basicCredential;


            MailMessage message = new MailMessage();

            MailAddress fromAddress = new MailAddress("[email protected]", "Me myself and I ");
            message.From = fromAddress;
            //here you can set address   
            message.To.Add("[email protected]");
            //here you can put your message details

            mailClient.Send(message);

Good luck..

Upvotes: 1

CodeMad
CodeMad

Reputation: 948

Can you try this out?

            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
                try
                {
                    MailAddress fromAddress = new MailAddress(ConfigurationManager.AppSettings["fromAddress"], ConfigurationManager.AppSettings["displayName"]); //Default from Address from config file
                    MailAddress toAddress = new MailAddress("[email protected]", "from sender");

                    // You can specify the host name or ipaddress of your server
                    // Default in IIS will be localhost 
                    smtpClient.Host = ConfigurationManager.AppSettings["smtpClientHost"];


                    //Default port will be 25
                    smtpClient.Port = int.Parse(ConfigurationManager.AppSettings["portNumber"]); //From config file

                    //From address will be given as a MailAddress Object
                    message.From = fromAddress;

                    // To address collection of MailAddress
                    message.To.Add(toAddress);
                    message.Subject = ConfigurationManager.AppSettings["Subject"]; //Subject from config file

                    message.IsBodyHtml = false;

                    message.Body = "Hello World";
                    smtp.DeliveryMethod = SmtpDeliveryMethod.NetWork                    
                    smtpClient.Send(message);
                }
                catch (Exception ex)
                {
                   throw ex.ToString();
                }

The configuration settings would be,

<configuration>
    <appSettings>
        <add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
        <add key="portNumber" value="25"/>
        <add key="fromAddress" value="[email protected]"/>
        <add key="displayName" value="Auto mail"/>
        <add key="Subject" value="Auto mail Test"/>
    </appSettings>

</configuration>

Upvotes: 0

Elias Hossain
Elias Hossain

Reputation: 4469

var smtpClient = new SmtpClient(gmailSmtpServer, gmailSmtpPort)
{
   Credentials = new NetworkCredential(FromGEmailAddress, FromGEmailAddressPassword),
   EnableSsl = true
};

try
{
    using (var message = new MailMessage(fromaddress, toaddress))
    {
      message.Subject = "Test";
      message.Body = "Testing this shit!";
      smtpClient.Send(message);
     return true;
    }
}
catch (Exception exc)
{
  // error
  return false;
}

Upvotes: -1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

As suggested on this page, try installing telnet to see if you can connect to the mail server. It could be a firewall issue on your server. You can also try using another port as suggested in the link.

Upvotes: 1

Grrbrr404
Grrbrr404

Reputation: 1805

You never set the login add this before your smtpClient.Send() Method.

NetworkCredential NetCrd = new NetworkCredential(youracc, yourpass);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = NetCrd;

Load the web.config via ConfigurationManager if it does not work automatically.

Upvotes: 2

Related Questions