How do i send email programmitcaly in c# - c#

I am trying to send email through gmail server but is giving exception.The exception thrown is --> SMTP server requires a secure connection or the client was not authenticated.The server response was 5.7.0.Authentication required.
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("account1#gmail.com");
mail.To.Add("account2#gmail.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
// mail.Attachments.Add(new Attachment("C:\\file.zip"));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("account1#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Related

Send mail from office 365 for business in C#

I tried sending mail in my ASP.NET CORE project by C#. I ran code in emulation (IIS Express) but I get an error:
The SMTP server requires a secure connection or the client was not authenticated.
Please can you help me?
My code:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("formail#mail.com", "John Doe"));
msg.From = new MailAddress("frommail#mail.com", "Jane Doe");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("frommail#mail.com", "Password");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
return Content("Message sent successfully");
}
catch (Exception ex)
{
return Content(ex.ToString());
}
Full error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [AM3PR07CA0113.eurprd07.prod.outlook.com]
Thank you very much.
EDIT:
I used code from this question. But I get error
System.Net.Sockets.SocketException (11001): No such host is known.
My mail adress is me#gjb-spgs.cz.
My new code:
var sClient = new SmtpClient("gjbspgs-cz.mail.protection.outlook.com");
var message = new MailMessage();
sClient.Port = 25;
sClient.EnableSsl = true;
sClient.Credentials = new NetworkCredential("me#gjb-spgs.cz", "Password");
sClient.UseDefaultCredentials = false;
message.Body = "Test";
message.From = new MailAddress("me#gjb-spgs.cz");
message.Subject = "Test";
message.To.Add(new MailAddress("someone#gmail.com"));
sClient.Send(message);
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}

C# smtp doesn't want to send an email

when sending an email, I get an exception:
Sending mail failed.
I've set the host to "smtp.gmail.com" and port to 587 but it doesn't work.
Code:
public void SendActivationLink(string email)
{
using (MailMessage mail = new MailMessage())
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Port = 587;
MailMessage message = new MailMessage();
MailAddress from = new MailAddress("myemail#gmail.com", "Voc");
message.From = from;
message.To.Add(email);
message.Subject = "Title";
message.Body = "Jello";
smtpClient.Host = "smtp.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypassword");
try
{
smtpClient.SendAsync(message, email);
}
catch (SmtpException ex)
{
throw new ApplicationException("exeption" + ex.Message);
}
}

Email sending using gmail works fine but domain mail server throws error?

Gmail works fine but domain mail server throws below error.
The server response was 5.7.1. Receipient address rejected : Access denied
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(textBox1.Text.Trim());
message.To.Add(new MailAddress(textBox5.Text.Trim()));
message.Subject = "Test";
message.Body ="Test Message";
smtp.Port = int.Parse(textBox4.Text.Trim());
smtp.Host = textBox3.Text.Trim();
smtp.EnableSsl = checkBox1.Checked;
smtp.UseDefaultCredentials = checkBox2.Checked;
smtp.Credentials = new NetworkCredential(textBox1.Text.Trim(), textBox2.Text.Trim());
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("Message sent");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
//chTextBox1.Text = "";
}
i tried enable both ssl and netrk crediential also. same error as above

C# Send E-Mail issue

I'm trying to send an e-mail in C#,and I'm having some issues.Whenever I try to send an e-mail,I get a message "Error: Failure to send mail".
Here is my code:
try
{
client.Host = "smtp.gmail.com";
client.Port = 465;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
MailAddress sendFrom = new MailAddress("from#domain.com");
MailAddress sendTo = new MailAddress("to#domain.com");
MailMessage msg = new MailMessage(sendFrom,sendTo);
msg.Subject = "Subject";
msg.Body = "Body";
client.Send(msg);
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message);
}
Also I have this declaration:
public SmtpClient client = new SmtpClient();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
Hope you can help me.
you can try this and make sure you are using valid login credential and you have internet connection:
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
Try this code
using System.Net.Mail;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("sender#gmail.com");
mail.To.Add("reciever#gmail.com");
mail.Subject = ("e mail subject");
mail.Body = ("message body");
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("sender's username", "sender's password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");

Error whilst sending an email

I am trying to send email using c# following is my code.
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("kmrizwan.shahid#gmail.com");
msg.To.Add("kmrizwan.shahid#gmail.com");//Text Box for To Address
msg.Subject = "Testinng subject"; //Text Box for subject
msg.IsBodyHtml = true;
msg.Body = "testing comment is here..";//Text Box for body
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("kmrizwan.shahid#gmail.com", "");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = msg;
client.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
Giving following exception
It's common for internet providers to block the port used by SMTP, except to their own outgoing mailserver. The reason is to prevent spamming.
If that is the case for you, you need to use the mail server of your internet provider instead of the GMail server.

Categories