Sending an Email through gmail SMTP Server (SSL) - c#

I know there's a lot of example on how to send an email using C# but I am really running into some issues and I can't make it to work.
I always get "Failure sending mail" error, Unable to connect to the remote server - No connection could be made because the active machine actively refused it (IP address here).
What does this error mean? And how do I fix this?
Any help will be much appreciated
Here's the code that I've been using: (although I already did try a lot of things)
string SendersAddress = "test#gmail.com";
string ReceiversAddress = "test1#gmail.com";
const string SendersPassword = "test-pass-here";
const string subject = "Testing";
const string body = "Hi This Is my Mail From Gmail";
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
smtp.Send(message);
}
catch (Exception ex)
{
}
Thanks!

It sounds like you're running into issues connecting to the SMTP server.
Make sure the firewall is open on port 25, and that you actually have an SMTP server running wherever you're trying to establish your connection.

You're trying to establish an SSL connection to gmail on port 587. That port must be used with TLS, not SSL.
Use port 465 instead.
Also note that the Timeout property is expressed in milliseconds, so 3000 can be a little short, depending on your network. Try using a more permissive value, like 30000.

Also worth checking that your anti-virus is not blocking port 25, I have been caught by that before!

Same for me in past few days,
Easy solution:
First check if everything is OK with Telnet, then try to do the same in C#.
Here is a good intro : http://www.wikihow.com/Send-Email-Using-Telnet.
Just beware that some servers use the EHLO command instead of HELO
EDIT:
take a look at the way that you can connect to SMTP server of Google here

Related

Issue with Google smtp ASPMX.L.GOOGLE.COM

I couldn't send mail using smtp ASPMX.L.GOOGLE.COM. I have domain domain.co.in and email address test#domain.co.in which is accessible in gmail. i Could send mail manually but not programmatically. throws below error.
Service not available, closing transmission channel. The server response was: 4.7.0 [61.16.142.134 15] Our system has detected that this message is
MailAddress ma_from = new MailAddress("test#domain.co.in", "fromName");
MailAddress ma_to = new MailAddress("jrao.XXXX#gmail.com", "fromName");
string s_password = "TestPwd";
string s_subject = "Test";
string s_body = "This is a Test";
SmtpClient smtp = new SmtpClient
{
Host = "ASPMX.L.GOOGLE.COM",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ma_from.Address, s_password)
};
using (MailMessage mail = new MailMessage(ma_from, ma_to)
{
Subject = s_subject,
Body = s_body
})
smtp.Send(mail);
Where did you get those settings? I don't think port 25 will work with SSL. Try port 587 and host "smtp.gmail.com"
The full response from Google would help. Was it perhaps "Our system has detected an unusual rate of unsolicited mail originating from your IP address. To protect our users from spam, mail sent from your IP address has been temporarily rate limited." Note that unless you're using a fixed-line internet connection from home, you're probably sharing an IP address with many other users.
Ensure you have a "strong" password
Ensure the "from" email address is your primary email address and not an alias.
Try Using Port 587
And host "smtp.gmail.com"
Specially You'll need to set your gmail account allow less secure apps to send mails. It's a security concern in gmail.
You can set this setting by folowing this link.
Allow Less Secure Apps
Hope this would help.. :)

Can't send email via gmail servers, operation times out

EDIT
Just a problem with the mail server not responding in time, I tried with a 3rd provider and it worked fine.
END EDIT
I'm trying to send a user registration email to my new users.
For testing purposes I switched to using Gmail's SMTP server for this as I figured maybe my mail provider is blocking the request somewhere but the problem persists.
All I know is that the SmtpClient.Send() call is timing out. Here's my code:
public void SendEmail(string To, string Subject, string Body)
{
SmtpClient smtp = new SmtpClient(server, port);
smtp.Credentials =
new NetworkCredential("<my google account>", "<my google password>");
smtp.EnableSsl = true;
using (MailMessage msg =
new MailMessage("<my private email account>", To, Subject, Body))
{
msg.IsBodyHtml = true;
smtp.Send(msg);
}
}
Here's the stack trace:
System.Net.Mail.SmtpException was caught
HResult=-2146233088
Message=The operation has timed out.
Source=System
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Ortund.Utilities.Mail.SendEmail(String To, String Subject, String Body)
in e:\Development\Base\Ortund.Utilities\Mail.cs:line 91
at Ortund.Utilities.ServiceMail.SendUserRegistrationEmail(User Source)
in r:\Users\Ortund\Documents\Visual Studio 2013\Projects\OrtundWeb\Utilities\ServiceMail.cs:line 102
at OrtundWeb.Controllers.UserController.Post(User Source)
in r:\Users\Ortund\Documents\Visual Studio 2013\Projects\OrtundWeb\Controllers\UserController.cs:line 63
InnerException:
Not particularly helpful... I figured maybe connection was failing because of SSL or credentials so I added those in with no change...
Anyone got any suggestions here because I'm all out of ideas.
They are blocking an application trying to send out emails as you (as they should be). Just send the emails out from your own server and set the replyTo property to your gmail email address and it will look like it came from your account.
I also use the GMail SMTP server for testing. I use the following in VB.NET
'*****For testing using GMail smtp server****** Comment out for production
'Smtpclient to send the mail message
sSMTP = "smtp.gmail.com"
SmtpMail.Port = 587 '587 'For some reason vb.net doesn't like port 465. Access does.
SmtpMail.EnableSsl = True
SmtpMail.UseDefaultCredentials = False
SmtpMail.Timeout = 30000 'An Int32 that specifies the time-out value in milliseconds. The default value is 100,000 (100 seconds).
SmtpMail.Credentials = New System.Net.NetworkCredential("username", "gmail account name")

Sending out an automated email

I have an service that needs to send out automated emails on failures. I feel like i have it set up but i keep on receiving the following error:
Service not available, closing transmission channel. The server response
was: 4.3.2 Service not available
I cant quite figure out where i went wrong, but here is my code:
public static void AutoEmail()
{
try
{
SmtpClient newClient = new SmtpClient();
newClient.Host = "host name";
newClient.Port = Port number;
newClient.Credentials = new System.Net.NetworkCredential(
"username", "password");
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("something#email.com"));
mail.Body = "This is a test message.";
mail.Subject = "Test - " + DateTime.Now;
mail.From = new MailAddress("something2#email.com");
newClient.Send(mail);
}
catch (Exception ex)
{
Log.WriteException("Error in Email", ex);
}
}
Any help would be much appreciated. Thanks!
Are you sure SMTP server you are using permits applications to send emails? I had met with similar issue and root cause was Exchange server was rejecting the send request due to insufficient permissions. And I have the same steps in my code as in yours. Check for permissions.
Have you tried
newClient .UseDefaultCredentials = true;
Does it help ?
The SMTP server name only works on a computer within the network that contains that SMTP server.
You need to make sure SMTP server's host name and port number in your program is correct. All the other code in your program seem fine.
I experienced the same error before. At the end, I changed to the correct host name and port number and everything works.
For example, Outlook.com Or Hotmail email accounts
host="smtp-mail.outlook.com" port="25" enableSsl="true"
https://www.outlook-apps.com/outlook-com-pop-settings/

how to test connection is established in smtp in c# windows

I want to test connection is established without sending mail.My code is below.I have put wrong email and password but i m not getting any exception.I don't want to test with email send.
try {
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential credentials = new NetworkCredential(textboxprimarymail.Text, textBoxpassprimary.Text);
smtpClient.Credentials = credentials;
connectionestablish.Text = "Connetion Established";
connectionestablish.ForeColor = System.Drawing.Color.ForestGreen;
}
catch {
connectionestablish.Text = "Connetion Error";
connectionestablish.ForeColor = System.Drawing.Color.Red;
}
This question is a duplicate. Basically, there is no way. As stated in another answer here:
There is no way.
SmtpClient can not validate
the usernamepassword without
contacting the serve it connects to.
What you could do is do it outside
SmtpClient, by opening a TCP
connection to the server... but
authentication CAN be complicated
depending how the server is
configured.
May I ask WHY you need to know that
before sending? normal behavior IMHO
would be to wrap sending in
appropriate error handling to catch
exceptions here.
How to validate smtp credentials before sending mail?

Why would SmtpClient behave differently on XP/Vista/Win7

I'm using the following code which appears to work perfectly every time on Vista/Win7.
private void SendEmail(string subject, string body, string attach)
{
using (MailMessage message = new MailMessage("username#gmail.com", "username#gmail.com", subject, body))
{
message.IsBodyHtml = true;
if (!string.IsNullOrEmpty(attach))
{
Attachment attached = new Attachment(attach);
message.Attachments.Add(attached);
}
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username#gmail.com", "password"),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
client.Send(message);
}
}
However on Windows XP I'm getting:
No connection could be made because the target machine actively refuses it
I've checked and Windows firewall is completely disabled...
Try from the Windows machine the following:
windows key + r
Type cmd
Type telnet smtp.gmail.com 587
If it says connection refused or similar then it's a firewall or network problem, unrelated with the code.
Hard to say if this is it, but we had that problem at one point, and it was an antivirus utility that was the culprit.
Are you using the same version of System.Net.Mail on all three systems ?
Also, could be related to the Windows Firewall blocking connections (or some other firewall).
I doubt this has anything to do with the OS, that type of exception is usually bubbled up from inner ones. Trap the exception and have a look into the inner exceptions and see what the real problem is.
However, this sort of problem is usually a firewall blockage, the remote smtp server is blocking incoming requests or your machine is blocking outgoing requests on port 25.

Categories

Resources