Not able to send email to other domains - c#

I have some code that can only work with my SMTP host domains but not others. It seems to send, but it doesn't actually send anything and it also doesn't throw any exceptions.
SmtpClient mailClient = new SmtpClient();
mailClient.UseDefaultCredentials = false;
mailClient.Port = 587;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Host = "mail.smtpserver.com";
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("mail#smtpserver.com", "password");
mailClient.Credentials = cred;
MailMessage message = new MailMessage();
message.From = new MailAddress("mail#smtpserver.com");
message.To.Add("mail#remotedomain.com");
message.Subject = "Subject";
message.Body = "Body";
mailClient.Timeout = 200000;
mailClient.Send(message);
I think the problem is not checking NetworkCredential...

I would suggest you to try to send a testing mail with the "other" smtp server using telnet to make sure it works.
your smtp port is not the default one so maybe it is blocked by your firewall.

Generally, if you're using port 587 then that is SSL secured SMTP - so you need to change your code so that uses it when establishing the connection:
mailClient.EnableSsl = true;

Related

sending the email using gmail smtp - unable to connect the remote server

I tried to send mail using Windows application in C#. I got code as below and tried but it is not working
It giving following error:
Unable to connect the remove server
Code:
MailAddress sendd = new MailAddress("xxxxxx#gmail.com","Sethu",Encoding.UTF8);
MailAddress receivee = new MailAddress("xxxxxx#yahoo.com");
MailMessage mail = new MailMessage(sendd, receivee);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxxxxx#gmail.com", "yyyyyyy");
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
Anyone help to find out what mistake i did?
can you try make your code to something like the following
var smptClient = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("smit123#gmail.com", "123456789"),
EnableSsl = true
};
smptClient.Send("smit123#gmail.com", "test#gmail.com", "Testing Email", "testing the email");
This code is properly tested with my own account on the Network..
Following code works for me
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("xxxxxx#gmail.com", "yyyyyyy"),
EnableSsl = true
};
client.Send("myusername#gmail.com", "myusername#gmail.com", "this is a test email.", "this is my test email body");
which is pretty much the same as yours with the difference that it doesn't use MailMessage object. This might not be the problem, but you could give it a try.
I would also check if you have port 587 enable on your server
You should also go through this topic where you find lots of useful tips

getting timeout when sending a mail in cshtml

I am trying to send a mail the form posts, but when the form posts it tries to send the mail and then times out. the code that is timing out is this:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("someone#example.com");
mail.To.Add("someoneElse#example.com");
SmtpClient smtp = new SmtpClient();
smtp.Port = 465;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.gmail.com";
mail.Subject = "Hello";
mail.Body = "World!";
smtp.Send(mail);
This is almost certainly an issue with your client setup.
If you're using port 465, you're required to use SSL -
See here https://support.google.com/a/answer/176600?hl=en
In your current client setup this is omitted.
Also, I think you're missing your credentials.
Try changing your client to
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true; //ssl is required
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your-email#gmail.com", "YourPassword");
client.Timeout = 20000; //increase the timeout
As mentioned in the comments, you may also need to allow "less secure apps" access to your account -
Info here http://support.google.com/accounts/answer/6010255

How to send Mail in asp.net from windows mail server

I have tried sending mail from gmail and other share hosting mail server and its works as expected
But Now my requirement is sending mail from windows mail server.whenever i try to send mail it shows gives me error failure sending mail.
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "us2.webmail.mailhostbox.com";
string fromaddress = "info#csi.com";
client.Port = 25;
//client.Host = "mail.csisite.com";
// setup Smtp authentication
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("info#csisite.com", "password");
//client.UseDefaultCredentials = true;
client.Credentials = credentials;
client.EnableSsl = false;
try
{
//mail For Customer
MailMessage msgcustomer = new MailMessage();
msgcustomer.From = new MailAddress(fromaddress);
msgcustomer.To.Add(new MailAddress(EmailID));
msgcustomer.Subject = "Thank you for writing to us" ;
msgcustomer.IsBodyHtml = true;
msgcustomer.Body = "Test Mail";
client.Send(msgcustomer)
Send Mail:
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;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
using System.Net.Mail
MailMessage msgMail = new MailMessage();
MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress("sender's email","sender`s name and surname");
myMessage.To.Add("recipient's email");
myMessage.Subject = "Subject";
myMessage.IsBodyHtml = true;
myMessage.Body = "Message Body";
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
mySmtpClient.Host = "your smtp host address";
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.ServicePoint.MaxIdleTime = 1;
mySmtpClient.Send(myMessage);
myMessage.Dispose();
updated user is getting the following error
{"A socket operation was attempted to an unreachable network 208.91.199.230:25"} and Error Code is 10051
The Error 10051 is a socket error that appears when the target network or host server can not be reached. The error could have occurred due to an erroneous configuration of the router, Internet disconnection, or a blocking action by a firewall. This error has occurred mostly in attempts to generate Internet Control Message Protocol (ICMP) and Simple Mail Transfer Protocol (SMTP) connections with wrong configurations. This error also indicates that the Windows software employed is configured to communicate to a router, since the 10065 error appears instead if Windows is not set to link to a router.
you should make ensure that the network path is correct, that the computer is not running two software firewalls and that the targeted computer is not turned off.
Use This Code for sending mail this is tested code.
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
} // Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
mSmtpClient.Credentials = new System.Net.NetworkCredential
("YourEmail#gmail.com", "Password");
//Or your Smtp Email ID and Password
mSmtpClient.EnableSsl = true;
mSmtpClient.Send(mMailMessage);
}
Note: I have been trying to send email through my ISP mail server with no success - following all the suggestions that I could find on these forums. I just discovered that it would work fine if I did not attempt to alter the default credentials setting. If I included either: client.UseDefaultCredentials = true; or client.UseDefaultCredentials = false; in my code, then I would get a login failure message from the server. By not including either, it worked fine.

send email C# smtpclient using ibm lotus

I have gone through all the answers ... this is my situation
i need C# code to send email using ibm lotus account ( have username and password)
the server from which our app sends out emails is authorized
no firewall stopping
IBM lotus client is not installed on the server. so cannot use interop.domino.dll
the SMTP service is exposed. i have ip address and port. cant telnet to it and test it becasue server does not have telnet and they will not allow us enabling it
When i run the code below i get connection actively refused exception.
Is there any working code sample .. or am i missing something here .. any trouble shooting tips will be appreciated.
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from.Text);
message.To.Add(new MailAddress(to.Text));
//message.To.Add(new MailAddress("recipient2#foo.bar.com"));
//message.To.Add(new MailAddress("recipient3#foo.bar.com"));
//message.CC.Add(new MailAddress("carboncopy#foo.bar.com"));
message.Subject = "Test email from cogniti";
message.Body = "Test email from Cogniti";
SmtpClient client = new SmtpClient();
client.Port = Convert.ToInt32(port.Text);
client.Host = smtp.Text;
client.Credentials = new System.Net.NetworkCredential(username.Text, passwordBox1.Password);
//client.UseDefaultCredentials = true;
if (ssl.Text.Equals("1"))
client.EnableSsl = true;
else
if (ssl.Text.Equals("2"))
client.EnableSsl = false;
else
client.EnableSsl = false;
client.UseDefaultCredentials = false;
client.Send(message);
MessageBox.Show("Message Sent to: " + to.Text);
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
MessageBox.Show(e3.InnerException.ToString());
MessageBox.Show(e3.Source);
MessageBox.Show(e3.StackTrace);
}
Have you tried to move
client.UseDefaultCredentials = false;
before
client.Credentials = new System.Net.NetworkCredential(...
Apparently, if UseDefaultCredentials property isn't set before you actually set client credentials, the AUTH command wont be sent.

Gmail: How to send an email programmatically

Possible Exact Duplicate: Sending Email in C#.NET Through Gmail
Hi,
I'm trying to send an email using gmail:
I tried various examples that I found on this site and other sites but I always get the same error:
Unable to connect to the remote server -- > System.net.Sockets.SocketException: No connection could be made because the target actively refused it 209.85.147.109:587
public static void Attempt1()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("MyEmailAddress#gmail.com", "MyPassWord"),
EnableSsl = true
};
client.Send("MyEmailAddress#gmail.com", "some.email#some.com", "test", "testbody");
}
Any ideas?
UPDATE
More details.
Maybe I should say what other attempts I made that gave me the same error:
(Note when i didn't specify a port it tryed port 25)
public static void Attempt2()
{
var fromAddress = new MailAddress("MyEmailAddy#gmail.com", "From Name");
var toAddress = new MailAddress("MyEmailAddy#dfdf.com", "To Name");
const string fromPassword = "pass";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
}
) { smtp.Send(message); }
}
public static void Attempt3()
{
MailMessage mail = new MailMessage();
mail.To.Add("MyEmailAddy#dfdf.com");
mail.From = new MailAddress("MyEmailAddy#gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("MyEmailAddy#gmail.com", "pass");
smtp.EnableSsl = true;
smtp.Send(mail);
}
I'm using the following code:
SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");//username doesn't include #gmail.com
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try {
sc.Send(mm);
} catch (Exception ex) {
EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
}
With the following code, it will work successfully.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#mydomain.com", "Enquiry");
mail.To.Add("abcdef#yahoo.com");
mail.IsBodyHtml = true;
mail.Subject = "Registration";
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("xyz#gmail.com", "<my gmail pwd>");
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
But, there is a problem with using gmail. The email will be sent successfully, but the recipient inbox will have the gmail address in the 'from address' instead of the 'from address' mentioned in the code.
To solve this, please follow the steps mentioned at the following link.
http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
before following all the above steps, you need to authenticate your gmail account to allow access to your application and also the devices. Please check all the steps for account authentication at the following link:
http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html
Here is my connection resource to connect to Gmail from Java
<!-- Java Mail -->
<Resource name="mail/MailSession" auth="Container" type="javax.mail.Session"
mail.debug="true"
mail.transport.protocol="smtp"
mail.smtp.host="smtp.gmail.com"
mail.smtp.user="youremail#gmail.com"
mail.smtp.password="yourpassword"
mail.smtp.port="465"
mail.smtp.starttls.enable="true"
mail.smtp.auth="true"
mail.smtp.socketFactory.port="465"
mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
mail.smtp.socketFactory.fallback="false"
mail.store.protocol="pop3"
mail.pop3.host="pop.gmail.com"
mail.pop3.port="995" />
Connect your Gmail account on Secure ports (465 for SMTP and 995 for POP3) and use any .NET SSL Factory available to connect securely to Gmail.
Are you sure that your GMail account is set up to allow POP/SMTP connections? It is a configurable option that you can turn on and off as you choose.
You can see my blog post here at http://codersatwork.wordpress.com/2010/02/14/sending-email-using-gmail-smtp-server-and-spring-mail/ which explains how to use spring mail for sending email via gmail smtp server.
I used java but you can see the configuration and use that in your c# code.
Try using port number 465 for SSL connection

Categories