Unable to connect to remote server while sending email in asp.net - c#

The above error arising while using email sending code my code is
string fromAddress = "mymail";
string fromPassword = "mypassword";
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
smtp.Send(fromAddress, toAddress, MailSubject, Body);
I googled many times but didnt get proper solution.
Port 587 is enbled and firewall blocking also not there.

try
{
MailMessage mail = new MailMessage();
mail.To.Add("sender id");
mail.From = new MailAddress("your id");
mail.Subject = "Mail from my web page";
mail.Body ="Body Content";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
//Or your Smtp Email ID and Password
smtp.Credentials = new System.Net.NetworkCredential
("XYZ", "XXXXX");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
//display exception
}

This code work for me.Try this.
MailMessage mM = new MailMessage();
mM.From = new MailAddress("YourGmail#gmail.com");
mM.To.Add(Email);
mM.Subject = "Your Sub";
mM.Body = "Your Body" ;
mM.IsBodyHtml = true;
mM.Priority = MailPriority.High;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential("YourGmail", "YourPassword");
//sC.EnableSsl = true;
sC.EnableSsl = true;
sC.Send(mM);

Thanks for the response.I got solution as
if you are using any antivirus software check it's log to see whether it is because of the antivirus. I faced same problem when McAffee was blocking my mails (there is a security policy - Prevent mass mailing worms from sending mails). Edit this policy and add your application to the exception list. In my case this sorted the problem. Please check if it works for you.

I use following code for Gmail:
Function SendMail_Gmail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String) As Boolean
Dim mailmsg As New System.Net.Mail.MailMessage()
mailmsg.From = New MailAddress(strFrom)
mailmsg.To.Add(strTo)
mailmsg.Subject = strSubject
mailmsg.IsBodyHtml = True
mailmsg.Body = strBody
mailmsg.Priority = System.Net.Mail.MailPriority.Normal
Dim client As New System.Net.Mail.SmtpClient()
client.Host = "smtp.gmail.com"
client.Port = "587"
client.Credentials = New System.Net.NetworkCredential("youremailid#gmail.com", "Yourpassword")
client.EnableSsl = True
Dim userstate As Object = mailmsg
client.Send(mailmsg)
Return True
End Function

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.

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

C# mails not being sent sometimes, but no error is thrown

This is my code :
public static void SendMail(string to, string subject, string body, string mailTitle)
{
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.From = new MailAddress("***", mailTitle);
mail.To.Add(to);
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient c = new SmtpClient("leavingstone.net", 25);
c.EnableSsl = false;
c.Credentials = new NetworkCredential("***", "***");
c.Send(mail);
}
Sometimes mails are sent, other times not - but no error is thrown.
Is it possible to fix the problem from code, or is it host fail?
Try my SMTP code, works perfect for me !
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.domain.com";
smtp.EnableSsl = Convert.ToBoolean("false");
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "no-reply#domain.com";
NetworkCred.Password = "**************";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = Convert.ToInt16(25);
smtp.Send(mm);

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

Categories