The goal is to send an email from a C# application. The code snippet below works for me, but when I run it at work (internet connection with a script configuration proxi.pac) it crashes.
Can someone help me?
string SendersAddress = "jd#gmail.com";
string ReceiversAddress = "dj#xxx.com";
const string SendersPassword = "xxxxxx";
const string subject = "Testing Gmail LOCAL";
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);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
Try with port number 25.
Hope it works.
Try Port 465 Seems like port 587 is correct.
Note that some ISPs and proxy servers block SMTP data when it's not using port 25. This may be a deal-braker for you.
UPDATE
Seems like your proxy server is blocking your communication.
It is interesting to note that a simple Google search cannot give a help page from Google that has the expected SMTP settings, although there are pages for POP and IMAP settings.
Related
I am trying to send email like this
var fromAddress = new MailAddress("fromaddress", "From Name");
var toAddress = new MailAddress("toaddress", "To Name");
const string fromPassword = "password";
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);
}
Console.WriteLine("Sent");
Console.ReadLine();
but it gives this error .
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required.
I am sing this code in simple console application on my local host . So whats the issue in my code ?
Update
I changed fromAddress email and it send email successfully . But i don't receive any email in my toAddress email's inbox/Spam .
Try to add DeliveryMethod = SmtpDeliveryMethod.Network when creating SmtpClient.
See post:
https://stackoverflow.com/a/489594/1432770
There is a variety of reasons for this discussed here:
Sending email through Gmail SMTP server with C#
Your code in the first link has worked for me.
Do you use two steps verification?
You need to sign in using application-specific passwords: https://support.google.com/accounts/answer/185833?hl=en
Your code worked for me too!
i use the following code to send email :
public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
try
{
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.datagts.net",
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = new System.Net.NetworkCredential("MeEmail#...", "Password")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = IsBodyHTML,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
}
return true;
}
catch
{
return false;
}
}
It works on local and when i use my web site under my local IIS but when i upload it to my website it does not work and does not send email even any error occurs.
is there anybody out there to help me about this ?
UPDATE1 : i remove the try catch and catch an error with this message : Failure sending mail
UPDATE2 : I change my stmp server and use my Gmail account , look at this code :
public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
try
{
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("MeEmail#gmail.com", "Password")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = IsBodyHTML,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
}
return true;
}
catch
{
return false;
}
}
and now i get an error yet :(
I get the "MustIssueStartTlsFirst" error that mentioned in this link.
I am now trying to check #EdFS point and use port 25
UPDATE3: It is because i use the shared server , i just can change the port to 25 , and steel it does not work an give the same error, I am trying to get support from my server backup team
Assuming the SMTP server (smtp.datagts.net) is running fine, some items to check:
Your code seems to be using UseDefaultCredentials=true, but on the next line your are providing credentials
As mentioned in the comments check that Port 587 isn't blocked at your web host
If you are hosted on a shared server (not a dedicated machine), it's likely ASP.Net is set for medium trust. IF so, you cannot use any port for SMTP other than Port 25.
Update:
To try and get to the error. In your LOCAL (development) machine, add this to your web.config:
<system.web>
...
<securityPolicy>
<trustLevel name="Medium" />
</securityPolicy>
...
ASP.Net on your local machine runs in FULL TRUST. The above setting makes the current web site/application you are working on run in medium trust. You can remove/comment as necessary. The point of this exercise is to try and match what your web host settings are (it's obviously different if things work in your local machine then dies when published). It would be nice to just obtain the info from your web host..but until then....
Then try both Port 587 and 25.
It should fail on port 587 with a security exception (because of medium trust)
If your mail server only accepts SMTP connections on port 587, then of course, port 25 will not work either (but you should get a different error). The point being "...it still doesn't work..." in this case is that the SMTP server (smtp.datagts.net) only accepts connections on port 587
GMAIL is the same story. You cannot use Port 587 if your web host settings for ASP.Net is medium trust. I have been through this many times - it will "work" in my local machine, but as soon as I enable medium trust in my local development machine, it will fail.
You should ask your web host what their settings are for ASP.Net - if its some "custom" setting you can ask for a copy and use that in your dev box as well.
I'm tired to send message over SSL with SMTP by i still have an exeption :
"Operation timed out"
My questions is :
How to resolve this problem;
How to send an email behind the firewall !!!
Thanks,
public void SendMessage()
{
SmtpClient client = new SmtpClient("servername.ru.alt001.com");
client.Credentials = new NetworkCredential("ali.mselmi#server.ru","password");
client.Port = 465;
client.EnableSsl = true;
MailMessage message = new MailMessage()
{
Subject = "Test Message",
Body = "Hello, this is a test !!! Kind Regards Ali Mselmi"
};
message.To.Add("ali.mselmi#gmail.com");
message.From = new MailAddress("ali.mselmi#server.ru");
client.Send(message);
}
}
That's the point of a firewall - people can't reach a port behind it unless they're allowed to.
You'll need to contact the firewall owner/admin if indeed that's the problem.
The other possibility is that you have the wrong port number - 465 is the legacy port number for secure smtp - I believe 587 is the recommended port...
Can we use gmail account to send email in asp.net website from *localhost * (local machine) ? I am trying but badly unsuccessful. It works fine on hosting but donot work on my machine.
I have windows server 2003 on my machine, I have added port 587 and 465 in firewall in exceptions. In my gmail account I also have enabled POP and IMAP. Some people suggest to use port 465 and others say port 587 should be used. I tried both and below was my result:
Using port 465 it take time and finally give message that the opration has timed out. falure
Using port 587 it dont take time, show message "failuer sending email" with an inner expection "No connection could be made because the target machine actively refused it 72.14.213.109:587"
Below is my code, please guide me where I am wrong or what I should do.
thanks
public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(gMailAccount);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Port = 587;
client.Send(msg);
return true;
}
catch (Exception e)
{
return false;
}
}
I use smtp.yandex.com. May be need change settings in your gmail account.
Try this firs: add:
client.DeliveryMethod = SmtpDeliveryMethod.Network;
If that does not help, your mailaddress might not be complete, use
var fromAddress = new MailAddress("from#gmail.com", "From Name");
and in the credentials use
Credentials = new NetworkCredential(fromAddress.Address, ....
Hope that helps
This SmtpClient has always worked for me:
new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("noreply#my_domain.com", "password")
};
And it matches yours.
Have you been able to contact smtp.gmail.com on port 587 through any other app? Telnet on that port maybe? Am thinking it's probably a network issue, although you did state that your firewall was wide open it's not the only hop between you and google.
I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.
I used this:
try
{
NetworkCredential login = new NetworkCredential("your_____#gmail.com", "password");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add(new MailAddress("my____email#gmail.com"));
email.From = new MailAddress("your_____#gmail.com");
email.Subject = "Question";
email.Body = question;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = login;
client.Send(email);
}
catch
{
}
But its giving me an SMTP error.
"Service not available, closing
transmission channel. The server
response was: Cannot connect to SMTP
server 209.85.129.111
(209.85.129.111:25), connect error
10051" System.Exception
{System.Net.Mail.SmtpException}
To send through your gmail account, you need to connect to port 587:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?
Gmail uses port 465 and the erros show port 25
try using 465 port
http://mail.google.com/support/bin/answer.py?answer=76147