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("mymail@gmail.com", "jub");
            myMailMessage.To.Add(new MailAddress("mymail@gmail.com", "Softden"));

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

and my web.config is:

<mailSettings>
      <smtp deliveryMethod = "Network" from="Jubair &lt;mymail@gmail.com&gt;">
        <network defaultCredentials="false" enableSsl="true" userName="mymail@gmail.com" 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

Community
  • 1
  • 1
jubair
  • 597
  • 1
  • 6
  • 23

4 Answers4

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("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "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("noreplyXYZ@gmail.com");
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("fromAddress@gmail.com", "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="from@gmail.com" deliveryMethod="Network">
        <network userName="from@gmail.com" 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="yourEmailAddress@gmail.com"/>
    </appSettings>
</configuration>
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Doesn't work. same exception – jubair Jan 24 '13 at 05:16
  • 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 – jubair Jan 24 '13 at 14:37
  • 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.. – MethodMan Jan 24 '13 at 15:56
0

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

alont
  • 263
  • 2
  • 3
  • 12
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.

Atif Imtiaz
  • 215
  • 2
  • 4
  • 24
0

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

Diggie
  • 142
  • 3
  • 10