I have created a Windows application which is used to send emails. i have given credentials. i turned on google/settings/lesssecure apps. Eventhough its not sending. Its showing the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required here is my code.
MailMessage message = new MailMessage();
message.From = new MailAddress("Selvacsebe23#gmail.com");
string[] mailaddress = new string[count];
int i;
if (textSubject.Text != string.Empty)
{
message.Subject = textSubject.Text;
if (textBody.Text != string.Empty)
{
message.To="Selvakesavan#gmail.com"
message.IsBodyHtml = true;
string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds";
message.Body = tmpBody;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("selvacsebe23#gmail.com", "mypassword");
message.Priority = MailPriority.High;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Mail has sent successfully !!!", "Success !");
}
else
{
MessageBox.Show("Please Enter Body of the Message !");
}
}
else
{
MessageBox.Show("Please Enter Subject !");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failure !");
log.Fatal(ex.Message);
}
}
If you turn on 2-step-verification, then you need to login using app-specific-password. You can create it here:
https://support.google.com/accounts/answer/185833?hl=en. If you use your normal password, then you will get exception : 5.5.1 Authentication Required. You don't need lots of code, this code is enough to send email without attachment:
const string from = "user1#gmail.com";
const string to = "user2#yahoo.com";
const string subject = "This is subject";
const string body = "This is body";
const string appSpecificPassword = "akdfkajsdhklakdfh";
var mailMessage = new MailMessage(from, to, subject, body);
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword);
smtpClient.Send(mailMessage);
}
Related
I am not able to send email from a xamarin.android app using MailKit library of jstedfast.
I am using the following code :
try
{
//From Address
string FromAddress = "from_sender#gmail.com";
string FromAdressTitle = "Email Title";
//To Address
string ToAddress = "to_receiver#gmail.com";
string ToAdressTitle = "Address Title";
string Subject = "Subject of mail";
string BodyContent = "Body of email";
//Smtp Server
string SmtpServer = "smtp.gmail.com";
//Smtp Port Number
int SmtpPortNumber = 587;
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
mimeMessage.Subject = Subject;
mimeMessage.Body = new TextPart("plain")
{
Text = BodyContent
};
using (var client = new SmtpClient())
{
client.Connect(SmtpServer, SmtpPortNumber, false);
// Note: only needed if the SMTP server requires authentication
// Error 5.5.1 Authentication
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate("from_sender#gmail.com", "password");
client.Send(mimeMessage);
Console.WriteLine("The mail has been sent successfully !!");
Console.ReadLine();
client.Disconnect(true);
}
}
catch (Exception ex)
{
string message = ex.Message;
}
When I run this code from my app, it throws an exception:
MailKit.Security.AuthenticationException
What I am missing in this code. Can anybody help me out !
Use MAILMESSAGE class.
using System.Net.Mail;
.
MailMessage mail = new MailMessage("example#gmail.com", "example#gmail.com", "Title","Body");
SmtpClient client = new SmtpClient();
client.Host = ("smtp.gmail.com");
client.Port = 587; //smtp port for SSL
client.Credentials = new System.Net.NetworkCredential("example#gmail.com", "password");
client.EnableSsl = true; //for gmail SSL must be true
client.Send(mail);
I tried to send mail from my asp.net web application.
I want to send my application password to the user mail id, for that I took the password from datatabase using getdetailss function.
btn4getPwd is the button calling the mailing function.
txtusername.Text is the text box containing sending mail address. All values are receiving correctly, no error occur but it's not working ..!
protected void btn4getPwd_Click(object sender, EventArgs e)
{
if (txtusername.Text.Trim() != "")
{
em.username = txtusername.Text.Trim();
DataTable forget = em.getdetailss(10);
string passwd = (forget.Rows[0]["PassCode"].ToString());
try
{
string Subject = "Your NLS Password";
string Body = passwd;
string ToEmail = txtusername.Text.Trim();
string SMTPUser = "mymail#gmail.com", SMTPPassword = "pswd";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(SMTPUser, "AspnetO");
mail.To.Add(ToEmail);
mail.Body = Body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);
smtp.EnableSsl = true;
smtp.Send(mail);
Response.Write("<script language=javascript>alert('Mail send...!! ')</script>");
}
catch (SmtpException ex)
{
lbl4get.Text = "SmtpException ";
}
catch (Exception ex)
{
lbl4get.Text = "Exception";
}
}
else { Response.Write("<script language=javascript>alert('Invalid USERNAME...!! ')</script>"); }
}
smtp.Port = 25; is default port but for as you are sending over SSL use port 587 or 465 (nonstandard, but sometimes used for legacy reasons). Assume NetworkCredential are correct.
The email is not being sent from C# code using the gmail smtp settings. It is giving an error of "Server requires a Secure connection or the Client was not authenticated."
The email and password are same as used for Login..
Following is the complete code.
MailMessage mail = new MailMessage("<from>","<to>");
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential("<email>", "<password>");
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Timeout = 20000;
client.UseDefaultCredentials = false;
mail.Body = "Test email from C# Code";
mail.Subject = "Test Email";
Console.WriteLine("Attempting to Send Email");
try {
client.Send(mail);
Console.WriteLine("Email sent... ");
}
catch (System.Net.Mail.SmtpFailedRecipientException ex) {
Console.WriteLine("Could not send email to the mentioned recipient" + ex.Message);
}
catch (System.Net.Mail.SmtpException ex) {
Console.WriteLine("Could not send Email..\n" + ex.Message + "\n" + ex.StackTrace);
}
Console.ReadLine();
Any help appreciated :) -- Kind regards,
You have to swap the following lines.
client.Credentials = new NetworkCredential("<email>", "<password>");
client.UseDefaultCredentials = false;
You have to set the UseDedaultCredentials property at first.
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("<email>", "<password>");
That's the way, it should work.
for sending email i set server name smtp.mail.yahoo.com and port is 465 i trying to send email but failed to send email
what is the correct servername and smtp port to send email using yahoo
what other configuration i needed to set ?
my code is here :
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(address);
message.Subject = subject;
message.From = new System.Net.Mail.MailAddress(from);
message.Body = body;
message.Bcc.Add(bcc);
message.CC.Add(cc);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.mail.yahoo.com");
smtp.Credentials = new System.Net.NetworkCredential(emailid,password);
smtp.Port = 465;
smtp.EnableSsl = true;
smtp.Send(message);
I just tried the code and I think the yahoo mail server does not use SSL, because if you comment out
//smtp.Port = 465;
//smtp.EnableSsl = true;
it works.
I am not confirm about your smtp server settings these are working fine for me..
replace your smtp server settings and have idea from this code snippet.
some of the smtp server standart settings are here:
http://www.emailaddressmanager.com/tips/mail-settings.html
//Send mail using Yahoo id
protected void Button2_Click(object sender, EventArgs e)
{
String frmyahoo = "fromid#yahoo.com"; //Replace your yahoo mail id
String frmpwd = "fromidpwd"; //Replace your yahoo mail pwd
String toId = txtTo.Text;
String ccId = txtCc.Text;
String bccId = txtBcc.Text;
String msgsubject = txtSubject.Text;
String mailContent = txtContent.Text;
try
{
MailMessage msg = new MailMessage();
msg.To.Add(toId);
MailAddress frmAdd = new MailAddress(frmyahoo);
msg.From = frmAdd;
//Check user enter CC address or not
if (ccId != "")
{
msg.CC.Add(ccId);
}
//Check user enter BCC address or not
if (bccId != "")
{
msg.Bcc.Add(bccId);
}
msg.Subject = msgsubject;
//Check for attachment is there
if (FileUpload1.HasFile)
{
msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
clear();
Label1.Text = "Mail Sent Successfully";
}
catch (Exception ex)
{
Label1.Text = "Unable to send Mail Please try again later";
}
}
I can't send a email message using gmail settings. i already tried client.Host ="localhost" it's working but not in client.Host ="smtp.gmail.com".. Please help me guys.. I need use client.Host ="smtp.gmail.com".. thanks
here's my C# code:
string from = "aevalencia119#gmail.com"; //Replace this with your own correct Gmail Address
string to = "aevalencia191#gmail.com"; //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to); mail.From = new
MailAddress(from, "One Ghost" ,System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ; mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient(); //Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "iseedeadpoeple");
client.Port = 587; // Gmail works on this port client.Host ="smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
} HttpContext.Current.Response.Write(errorMessage
);
} // end try
here's the error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Much thanks guys!
You need to get and send mail to GMail by using SSL secutiry certificate
MailMessage msgMail = new MailMessage("a#gmail.com", "b#mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a#gmail.com", "a");
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
}
reference: http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
if (to == "")
to = "aevalencia119#gmail.com";
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = new MailAddress( "aevalencia119#gmail.com");
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = new MailAddress("aevalencia119#gmail.com");
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
client.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("aevalencia119#gmail.com", "####");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}