Reputation: 24766
One of my windows application need to send and receive email using POP and SMTP protocols. Our email server would be in outside of the company. So simulate this situation currently we uses Gmail account to send and receive email.As I know it needs proxy details to connect to a server outside trough proxy. I tried to configure proxy details in app.config file. But it doesn't works. It throws exception while email sending Failure sending email
. Someone have idea how to connect to external email server trough proxy?
My code
var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "nayana12345";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
try{
smtp.Send(message);
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="False" proxyaddress="http://123.456.78.90:8000" bypassonlocal="True" autoDetect="False" />
</defaultProxy>
</system.net>
</configuration>
Upvotes: 1
Views: 3650
Reputation: 72
As suggested port 587 for gmail.
It is possible that it isnt possible to send using SMTP through your corporate network/proxy and it is blocked. We have that issue and they way around is to use an SMTP relay that you can use to send mail without your program needing to be proxy aware.
It could be worth checking you can get out by using telnet? http://www.port25.com/how-to-check-an-smtp-connection-with-a-manual-telnet-session-2/
Another possibility is that an anti-virus client is blocking the program from sending email (McAffe is particularly good at this), try disabling all AV protection before running the program.
Upvotes: 2