0

Possible Duplicate:
send email asp.net c#

I have sent mail several times using the technique several times before but somehow it doesnt work i am providing the code in the following:

MailMessage myMailMessage = new MailMessage();
            myMailMessage.Subject = "Response From Client";
            myMailMessage.Body = "hello word";
            myMailMessage.From = new MailAddress("[email protected]", "jub");
            myMailMessage.To.Add(new MailAddress("[email protected]", "Softden"));

            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Send(myMailMessage);

and my web.config is:

<mailSettings>
      <smtp deliveryMethod = "Network" from="Jubair &lt;[email protected]&gt;">
        <network defaultCredentials="false" enableSsl="true" userName="[email protected]" password="Mypassword" host="smtp.gmail.com" port="587"></network>
      </smtp>
    </mailSettings>

it says the smtp server requires a secure connection or the client was not authenticated.. Please help

3
  • you need to set credentials with username, password Commented Jan 24, 2013 at 5:06
  • @DJKRAZE: userName="[email protected]" password="Mypassword" Commented Jan 24, 2013 at 5:07
  • I added a simple example that I just tested on my machine and it worked look at the second example as well and see if you can utilize it Commented Jan 24, 2013 at 5:26

4 Answers 4

4

Try adding something like this Per Dominic's answer on Stackoverflow look at he following example

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               //UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

//----------------- A simple approach below --------------- I just tested this below and it works

var mail = new MailMessage();

// Set the to and from addresses.
// The from address must be your GMail account
mail.From = new MailAddress("[email protected]");
mail.To.Add(new MailAddress(to));

// Define the message
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = message;

// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";//ForGmail
mailclient.Port = 587; //ForGmail


// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;//ForGmail
//mailclient.EnableSsl = false;
mailclient.UseDefaultCredentials = true;

// Specify your authentication details
mailclient.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxx123");//ForGmail
mailclient.Send(mail);
mailclient.Dispose();

//The .config settings there are two options on how you could set this up I am suspecting that this is the issue you are having

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

or option 2

<configuration>
    <appSettings>
        <add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
        <add key="portNumber" value="587"/>
        <add key="fromAddress" value="[email protected]"/>
    </appSettings>
</configuration>
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't work. same exception
nicely work... but one thing i really dont understand. I use my another gmail id and it works nicely and my code also works nice. but with my first gmail id doesnt neither yours nor mine.. Thanks a lot by the way.. Your post is really helpful
The password could have been locked on the gmail side from trying too many times to validate an invalid password .. kind of like when you try to log onto your network more than 3 times with an invalid password, you get locked out..
0

Send Email from Yahoo, Gmail, Hotmail (C#)
http://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-Gmail-Hotmail-Csharp

Comments

0

These are evry good tutorials with samples. make your demo email id. pass your id and its password as parameters. and send mail to anyone.

http://www.codeproject.com/Articles/15807/Easy-SMTP-Mail-Using-ASP-NET-2-0

http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

http://www.codeproject.com/Tips/490604/Sending-mail-using-ASP-NET-with-optional-parameter

if you are still unable to send email make sure to change the port number. but 587 should work normally.

Comments

0

Make sure you contact the email server side to see what kind of authentication they accept to relay.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.