my mail sender codes fails with gmail lately - c#

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?

Related

System.Net.Mail and MailMessage not Sending Messages

I have update from an obsolete name space to System.Net.Mail - the code was supposed to be straight forward - I am having problems sending email and can't get hold of the issue
public bool send()
{
SmtpClient mailClient = new SmtpClient("my domain", 25);
MailMessage mailMessage = new MailMessage();
mailClient.EnableSsl = false;
mailMessage.From = new MailAddress("my email");
mailMessage.To.Add(sendTo.ToString());
//mailMessage.Bcc.Add(bcc.ToString());
//mailMessage.CC.Add(cc.ToString());
mailMessage.Subject = subject;
mailMessage.Body = body.ToString();
mailMessage.IsBodyHtml = true;
try
{
mailClient.Send(mailMessage);
}
catch (Exception exp)
{
exp.ToString();
return false;
}
return true;
}
using Gmail smtp server
var client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
if (!client.UseDefaultCredentials)
client.Credentials = new System.Net.NetworkCredential("your_email#mail.com", "your_email_pass");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
var mail = new MailMessage("from#mail.com", "to#mail.com");
mail.Subject = "test ";
mail.Body = "body message";
mail.IsBodyHtml = true;
client.Send(mail);
No Credential for the smtp server? Or you add it already.
Try
mailClient.Credentials = new NetworkCredential("username", "password"),

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

Send Email With C# (Cpanel Hosting)

I have bought a cPanel host and the SMTP server information is:
This is my code:
string smtpAddress = "mandane.hostcream.com";
int portNumber = 465;
bool enableSSL = true;
string emailFrom = "mahabadi#exirsec.ir";
string password = Authenitication.PassWord;
string emailTo = To.Text;
string subject = Subject.Text;
string body = Body.Text;
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);
}
}
When I run my code and click on the send button after 1 or 2 minutes this appears:
Additional information: Failure sending mail.
What am I doing wrong?
I think you missed something, try this:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential smtpCredentials = new NetworkCredential("email from","password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("email from");
MailAddress toAddress = new MailAddress("email to");
smtpClient.Host = "smpt host address";
smtpClient.Port = your_port;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = smtpCredentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 20000;
message.From = fromAddress;
message.To.Add(toAddress);
message.IsBodyHtml = false;
message.Subject = "example";
message.Body = "example";
smtpClient.Send(message);
It seems that you cannot reach Yahoo address on port 465, please check if this address reachable first because it appears to be a network issue.

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());
}

How to add smtp hotmail account to send mail

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();
}
}

Categories