I've been making a Real Time Modding Tool for Call of Duty and am trying to make a report bug system, but I'm getting this error:
the codes that I'm using for this are as follows:
private void button4_Click(object sender, EventArgs e)
{
// Create the mail message
MailMessage mail = new MailMessage();
// Set The Addresses
mail.From = new MailAddress("brinkerzbhtests#gmail.com");
mail.To.Add("brinkerzbhtests#gmail.com");
// Login to that email
// Set The Content
mail.Subject = "RTM Tool Bug";
mail.Body = textBox1.Text;
// Send The Message
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential info = new NetworkCredential("brinkerzbhtests#gmail.com", "PasswordNotBeingGivenHere");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Picture of full help screen:
NetworkCredential info = new NetworkCredential("brinkerzbhtests#gmail.com", "PasswordNotBeingGivenHere");
smtp.Credentials =info ; // add this line
You create your network credentials and don't associate them with the smtp client.
Try adding the line:
smtp.Credentials = Info;
Related
I need to send list box items to email. Code is running fine, but when it gets to Send method it fails to send it.
protected void ProcessButton_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone#live.com");
mailMessage.From = new MailAddress("myAdress#live.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = OrderListBox.Items.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.live.com");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send email - error: " + ex.Message);
}
}
You can write list in file and send it as attachment (gmail example):
protected bool ProcessButton_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("myEmail#gmail.com", "password");
//if you have double verification on gmail, then generate and write App Password
client.EnableSsl = true;
MailMessage message = new MailMessage(new MailAddress("myEmail#gmail.com"),
new MailAddress(receiverEmail));
message.Subject = "Title";
message.Body = $"Text";
// Attach file
Attachment attachment;
attachment = new Attachment("D:\list.txt");
message.Attachments.Add(attachment);
try
{
client.Send(message);
// ALL OK
return true;
}
catch
{
//Have problem
return false;
}
}
and write this on begining of code:
using System.Net;
using System.Net.Mail;
You can choose smpt host adress and port if you want.
I saw all the other pages on stackoverflow that were about this problem and tried them but none of them worked.
im doing a website as a project for school, and I want the users to send an e-mail for them to report problems in, but it always gives me that error.
this is my code:
protected void Sendbtn_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage = new MailMessage("user#hotmail.com","my#hotmail.com");
mailMessage.Subject = "Problem from Gamer's Utopia";
mailMessage.Body = this.msgtxt.Text;
SmtpClient smtpClient = new SmtpClient(" smtp.live.com");
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
}
I tried using authentication with username and password but it didnt work - unless I did it incorrectly.
add authenticated NetworkCredential
System.Net.NetworkCredential smtpUser = new System.Net.NetworkCredential("admin#hotmail.com", "password");
smtpClient.Credentials = smtpUser;
You need to set SmtpClient Credentials, for example
smtpClient.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
check below answer for sample code:
https://stackoverflow.com/a/9851590/2558060
Change your code according to this!!! It will perfectly work!!!
using (MailMessage mail = new MailMessage())
{
SmtpClient client = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("from address");
mail.Subject = "";
string message = "";
mail.Body = message;
try
{
mail.To.Add(new MailAddress("To Address"));
}
catch
{
}
client.Credentials = new System.Net.NetworkCredential("smtp.live.com", "password");
client.EnableSsl = true;
try
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.Send(mail);
mail.To.Clear();
}
catch (Exception ex)
{
}
}
From your sender account, view the email inbox and say it is you to allow the third party to send emails.
I'm sending simple email messages in my application using smtp client and I was using this code before and it just works fine. Now, when I tried to run my project again from my local host computer and try to send email messages. I got a runtime error that says
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
I don't know what just happened since it was working fine before. Now I can't send email and all I've got is this error. I could hardly troubleshoot what went wrong. How do I resolve this? Here's my code below: Thanks...
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(#"myemailaddress#gmail.com",#"myemailpassword");
// create message
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox4.Text);
message.To.Add(new MailAddress(TextBox1.Text));
message.Subject = TextBox2.Text;
message.Body = TextBox3.Text; //body of the message to be sent
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
// message.Subject = "subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
try
{
client.Send(message);
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Mail has been successfully sent!')", true);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
message.Dispose();
}
Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.
Also please go to this link and click on Continue Allow access to your Google account
also I edit it little bit :
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
before asking i search some possible solution in this site.
I found this Solution that I implemented in my code
But I have an error: when I send a mail with my code (in localhost) I obtain "Invalid Handler" but username and password are correct. Someone can help me?thanks
This is the cdoce
private SendGmail(){
try{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail#gmail.com", "Enquiry");
mail.To.Add(TextBox_mail.Text.ToString());
mail.IsBodyHtml = true;
mail.Subject = "Test";
mail.Body = "Some Text";
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("mymail#gmail.com", "password");
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
}
catch (Exception errore)
{
elenco_errori.Visible = true;
elenco_errori.InnerText = errore.Message;
}
}
I solved the question
in Application Start, in global.asax I added this code
public void Application_Start() {
System.Security.Cryptography.RSACryptoServiceProvider.UseMachineKeyStore = true;
System.Security.Cryptography.DSACryptoServiceProvider.UseMachineKeyStore = true;
}
here is the code.
void sendMail()
{
MailMessage mail = new MailMessage();
mail.To.Add("abc#gmail.com");
mail.From = new MailAddress("abc#gmail.com", txt_name.Text);
mail.Subject = txt_subject.Text;
mail.Body = txt_body.Text;
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
NetworkCredential yetki = new NetworkCredential("abc#gmail.com", "11111111");
smtp.Credentials = yetki;
smtp.Send(mail);
Response.Write("mailiniz başarılı bir şekilde gönderilmiştir");
}
protected void btn_gonder_Click(object sender, EventArgs e)
{
sendMail();
}
Use a try/catch block
void sendMail() {
try{
MailMessage mail = new MailMessage();
mail.To.Add("abc#gmail.com");
mail.From = new MailAddress("abc#gmail.com", txt_name.Text);
mail.Subject = txt_subject.Text;
mail.Body = txt_body.Text;
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
NetworkCredential yetki = new NetworkCredential("abc#gmail.com", "11111111");
smtp.Credentials = yetki;
smtp.Send(mail);
Response.Write("mailiniz başarılı bir şekilde gönderilmiştir");
}
catch(Exception e){
Response.Write(e.Message);
}
}
The error indicating it is unhandled means you didn't catch the exception. Now what you actually do in the catch block to handle the exception is up to you, such as logging it to a file, queuing a retry, or showing a message box. Or trying to do something to prevent the exception in the first place.
protected void btn_gonder_Click(object sender, EventArgs e)
{
try{
sendMail();
}
catch(Exception ex)
{
}
}
Also note you can access ex.Message to see the exception message, or add a break point to the catch block and inspect ex. It is possible there is more you need to do to get it to work with gmail because of the requirements for authentication. I don't know if the network credentials are enough. I've always had trouble with it and resorted to using my ISP's email account that doens't require authentication.
You add a try/catch block to your code and ignore the error, or you post the entire exception message so that we can help you solve the actual problem.
According to this page when sending mail over SSL you should use port 465 i.e.
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
smtp.Port = 465;
...
You should probably still wrap the call to Send in a try/catch block and handle any SmtpException that gets thrown if you can do anything about it.
Note you can also put smtpClient configuration in the web.config file (see here for example).
You haven't specified the port number for Gmail which is 587.
smtp.port = 587;
And your problem should be solved. Otherwise your code looks good.