How to add smtp hotmail account to send mail - c#

I wrote some codes so as to send e mail but I can only send mail from gmail account to gmail account also, I want to use hotmail accounts how can i do it? thanks
It is
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("xxx#gmail.com");
mail.To.Add("kalaylevent#gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("kalaylevent#gmail.com", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

I changed a little of code and it tested successfully (from Hotmail to Gmail)
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("youremail#hotmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

I use different smtp client and make sure to set the socket options to StartTls :
using (SmtpClient client = new())
{
try
{
await client.ConnectAsync("smtp.office365.com", 587, SecureSocketOptions.StartTls);
client.AuthenticationMechanisms.Remove("XOAUTH2");
await client.AuthenticateAsync("Your_User_Name", "Your_Password");
await client.SendAsync(mailMessage);
}
catch
{
//log an error message or throw an exception, or both.
throw;
}
finally
{
await client.DisconnectAsync(true);
client.Dispose();
}
}

Related

Why do I get an error by trying to send a mail with c#?

I'm trying to send an e-mail using C#, but I get this recurring error :
.
Can you explain me what's wrong with my code ?
Here it is :
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myEmailAdress#gmail.com", "myPassword");
MailMessage msg = new MailMessage();
msg.To.Add("receiver#gmail.com");
msg.From = new MailAddress("sender#gmail.com");
msg.Subject = "My subject";
msg.Body = "My body";
client.Send(msg);
MessageBox.Show("Message sent !");
I have encountered same before.
You are getting this error because you haven't set ON on Less secure app access for sender#gmail.com as you are using Gmail SMTP port.
Reason:
Your email has no remotely access permission. You have to configure
it. Suppose you want to sent email from sender#gmail.com so you
have set that permission NO to this account.
How To Set:
You could try like below
Or can open that tab from this link directly Less secure app access
Update:
As per your comment this is for you which has working perfectly since the beginning of my career
public object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
// mail.Attachments.Add(new Attachment(#attacmmentLocationPath));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Hope that would help.
This code works for gmail, it is very similar to yours with slight differences, but if you try this, and it doesn't work for you, the issue is not the code - perhaps some other network related issue that you are going to need to fix first:
using (var msg = new MailMessage())
{
msg.From = new MailAddress("fromaddress#gmail.com");
msg.To.Add("toaddress#gmail.com");
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("xxx#gmail.com","password");
smtp.Send(msg);
}
}
An SMTP client may log in using an authentication mechanism chosen among those supported by the SMTP servers.

Not able to send E-mail through IIS server (Working in local )

I've been working on this from months, please someone help me solve this. Thanks
try
{
SmtpClient smtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
smtpServer.Credentials = new System.Net.NetworkCredential("iejabdb#otlook.com", "throughput");
smtpServer.Port = 25;
smtpServer.Host = "*****";
smtpServer.EnableSsl = true;
smtpServer.UseDefaultCredentials = true;
mail.From = new MailAddress("*****#outlook.com", "Work Order System");
mail.To.Add("*******#outlook.com");
// mail.To.Add(receiptsArray.ToString());
mail.Subject = "Test";
mail.Body = "Hello";
smtpServer.Send(mail);
}
Test this Code:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("mawlud.f.m#hotmail.com");
mail.To.Add("mawlud.f.m#gmail.com");
mail.Subject = "Mail Subject";
mail.Body = "Mail Body";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("mawlud.f.m#hotmail.com", "Write Your Password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

my mail sender codes fails with gmail lately

I used this code to sending mail with gmail! before it works very well but now it fails! (it is in c# and form application)
cmdSend.Text = "Wait!";
cmdSend.Enabled = false;
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
var mail = new MailMessage();
mail.From = new MailAddress(txtMailAddressFrom.Text);
mail.To.Add(txtMailAddressTo.Text);
mail.Subject = txtSubject.Text;
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = txtMailBody.Text;
mail.Body = htmlBody;
SmtpServer.Port = 578;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(txtMailAddressFrom.Text, txtPassword.Text);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
SmtpServer.Dispose();
cmdSend.Text = "Send";
cmdSend.Enabled = true;
I tested with port 465 but don't work!
Doesn't Gmail use port 587 and not 578?

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");

Send gmail and live/hotmail emails

For my application I have code to send emails for live/hotmail, but not for gmail, that doesn't work I tried to build a check for it, to see what account is used to send the e-mail but it's not working, when I try to send an Gmail email. Here is the code I use for the check:
MailMessage msg = new MailMessage();
msg.To.Add(txtAan.Text);
msg.From = new MailAddress(txtGebruikersnaam.Text);
msg.Subject = txtOnderwerp.Text;
msg.Body = txtBericht.Text;
string smtpcheck = txtGebruikersnaam.Text;
smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));
SmtpClient smtp = new SmtpClient();
if (smtpcheck.ToLower() == "#gmail.com")
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
}
else if(smtpcheck.ToLower() != "#gmail.com")
{
smtp.Host = "smtp.live.com";
smtp.Port = 587;
}
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(txtGebruikersnaam.Text, txtWachtwoord.Text);
smtp.Send(msg);
This code gives me an error when I try to send an E-mail by using Gmail, can somebody give me a little help with this problem? And yes I also tried the port: 465 and 587 for gmail, so I don't think that is the problem either.
This line doesn't change the value of smtpcheck
smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));
you need to write
smtpcheck = smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));
As result your if condition fails and you fall to send the mail always with live.com
EDIT: For gmail, this code is confirmed to work
SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

Categories