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("nayana@gmail.com", "From Name");
var toAddress = new MailAddress("nayana@gmail.com", "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>