sending email using gmail from asp.net - c#

I am trying to send mail using gmail in asp.net
My Code:
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
But i am getting error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
where is my error?

You can try the below suggestions:
Try setting "UseDefaultCredentials" to "False" smtp.UseDefaultCredentials = false;
Try logging to your Gmail account, it may be blocking the access.
Try enabling access to your Gmail account, see this link for more information http://email.about.com/od/gmailtips/qt/How-To-Unlock-Gmail-For-A-New-Email-Program-Or-Service.htm

Change "smtp.UseDefaultCredentials = true;" to "smtp.UseDefaultCredentials = false;". Or test your gmail account with code below.
using (MailMessage mm = new MailMessage(new MailAddress("sender#example.com"), new MailAddress("recipient#example.com")))
{
mm.Subject = "test";
mm.Body = "body test";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential NetworkCred = new NetworkCredential("sender#example.com", "password");
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
UPDATE
Go to: google.com/settings/security?hl=en_GB and if field "Access for less secure apps" is Disabled click settings and change it to enabled... Then test your connection with code above. It worked for me on new gmail user

Related

c# MimeKit mail not sent without error. System.Net.Mail works fine

I made a console application to test MimeKit, it's executed without errors but emails never arrives.
i usae same data with System.Net.Mail and all works fine.
What can be the problem?
MimeKit code:
var email = new MimeMessage();
email.Sender = MailboxAddress.Parse("aa#sbb.com");
email.To.Add(MailboxAddress.Parse("aa.cc#dd.com"));
email.To.Add(MailboxAddress.Parse("bb.dd#dd.com"));
email.Subject = "test";
var builder = new BodyBuilder();
builder.HtmlBody = "corpo";
email.Body = builder.ToMessageBody();
using var smtp = new SmtpClient();
smtp.Connect("smtp.xxx.com", 25, SecureSocketOptions.None);
smtp.Authenticate("ut", "pwd");
smtp.SendAsync(email);
smtp.Disconnect(true);
Console.WriteLine("Email Sent.");
System.mail code:
using (MailMessage mm = new MailMessage())
{
mm.To.Add(new MailAddress("aa.cc#dd.com"));
mm.To.Add(new MailAddress("bb.dd#dd.com"));
mm.From = new MailAddress("aa#sbb.com", "You");
mm.ReplyToList.Add("aa#sbb.com");
mm.Subject = "vecchia mail";
mm.Body = "con system.mail";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.xxx.com";
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential NetworkCred = new NetworkCredential("ut", "pwd");
smtp.Credentials = NetworkCred;
smtp.Port = 25;
Console.WriteLine("Sending Email......");
smtp.Send(mm);
Console.WriteLine("Email Sent.");
}
Thanks to #Klaus Gütter i got the error and then find the solution: a security rule that prevent connection between development machine and mail server, once deployed the application in test environment all works fine.
thanks for suggestions

Error when sending an email c#

I try to send an email (with gmail) in C#. I check the solutions I can find on google but it never works for me, I always have the error :
SMTP server requires a secure connection or the client was not
authenticated. The response from the server Was: 5.5.1 Authentication
Required.
This is an exampe of code :
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "*****#gmail.com";
string password = "*****";
string emailTo = "******#gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
The MSDN says that : Some SMTP servers require that the client be
authenticated before the server sends e-mail on its behalf. Set this
property to true when this SmtpClient object should, if requested by
the server, authenticate using the default credentials of the
currently logged on user. For client applications, this is the desired
behavior in most scenarios.
So you need to set smtp.UseDefaultCredentials = false; before assigning Credentials, So your code will looks like the following:
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
I find the solution which works for me. The solution proposed by un-lucky
was good but I need to configure gmail account.
So the code is :
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "****#gmail.com";
string password = "*****";
string emailTo = "****#gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Moreover in your gmail account, you have to make this changes.
gmail account changes

send email of any domain through aspx page

string from = txtEmail.Text;
using (MailMessage mm = new MailMessage(txtEmail.Text, "xyz#gmail.com"))
{
mm.Subject = "zaca";
mm.Body = " vfsdfs ";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
im using this code to send mail through my web page. but it can send mail to the admin through a gmail account only. i want that a user of any email service could send mail through my webpage. please help.

Not able to send emails through my asp.net application

I am getting Time-out Error.
I am not able to send email using this code. I tried almost every
possible way but of no use.
try
{
string myString = "Hello I am new email message. I am delivered for testing.";
MailMessage mm = new MailMessage("XXXXXXXXXX", "XXXXXXXXXX");
//mm.CC.Add("XXXXXXXXXX");
mm.Subject = "Subject have been successfully placed.";
mm.Body = myString.ToString();
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "XXXXXXXXXX";
NetworkCred.Password = "XXXXXXXXXX";
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 465;
smtp.Send(mm);
Request.Cookies.Clear();
}
catch (Exception ee)
{
Response.Write(ee.Message);
}
try this
private void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mail.From = new MailAddress("Setfromaddress");
mail.To.Add(new MailAddress("recepient#gmail.com"));
mail.Subject = "Test";
mail.Body = "This is a test";
mailClient.EnableSsl = true;
mailClient.Credentials = new NetworkCredential("Username", "Password");
try
{
mailClient.Send(mail);
}
catch(Exception ex)
{
WriteErrorOutput(ex.Message);
}
}
also I think we need to check the connection
To run the telnet and test on a Windows in your computer:
1.Open the Start menu, and select Run.
2.Enter command in the Open: field, and click OK.
3.Enter 'telnet smtp.gmail.com 465,' and hit Enter, or enter 'telnet smtp.gmail.com 587' instead.

exception while sending mail multiple recipient

I am trying to send an email to Multiple email addresses. But I am getting this exception
A recipient must be specified
at this line
smtp.Send(mail);
I have checked related posts on stackoverflow then
I have tried to add this line
mail.To.Add(new MailAddress(c[0].Address));
but this time exception message says
: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated
This is my code:
MailMessage mail = new MailMessage();
mail.From = new MailAddress(emailFrom);
MailAddressCollection c = new MailAddressCollection();
c.Add(new MailAddress("sendto#gmail.com"));
mail.Subject = "some word";
mail.Body = "some word";
mail.IsBodyHtml = true;
if (c[0].Address.Contains("yahoo"))
{
using (SmtpClient smtp = new SmtpClient())
{
smtp.Credentials = new NetworkCredential(emailFrom,PASS);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
else if (c[0].Address.Contains("hotmail"))
{
using (SmtpClient smtp = new SmtpClient())
{
smtp.Port = 587;
smtp.Host = "smtp.live.com";
smtp.Credentials = new NetworkCredential(emailFrom, PASS);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
else if (c[0].Address.Contains("gmail"))
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(emailFrom,PASS)
smtp.EnableSsl = true;
smtp.Send(mail);
}

Categories

Resources