[英]Sending email over proxy using CDO
I want to send email from C# winform application, my internet connection uses a proxy. 我想从C#winform应用程序发送电子邮件,我的互联网连接使用代理。
This is what i have done so far 这是我到目前为止所做的
WebProxy proxy = WebProxy.GetDefaultProxy();
Console.WriteLine(proxy.Address);
if (proxy.Address!=null)
{
try
{
MailMessage oMsg = new MailMessage();
// TODO: Replace with sender e-mail address.
oMsg.From = fromGmailAddress;
// TODO: Replace with recipient e-mail address.
oMsg.To = toAddress;
oMsg.Subject = "Send Using Web Mail";
// SEND IN HTML FORMAT (comment this line to send plain text).
oMsg.BodyFormat = MailFormat.Html;
// HTML Body (remove HTML tags for plain text).
oMsg.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>";
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/urlproxyserver", proxy.Address.Host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/proxyserverport", proxy.Address.Port);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", gmailUsername);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", gmailPassword);
SmtpMail.SmtpServer.Insert( 0,"smtp.gmail.com");
SmtpMail.Send(oMsg);
oMsg = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
It throws an exception, transport failed to connect to the server. 它将引发异常,传输无法连接到服务器。 I have tried 465,587 and 25 for port of Gmail smtp server. 我已尝试465,587和25作为Gmail smtp服务器的端口。 Nothing works. 什么都没有。
I had been reading over the net about the possibilities that proxy server may not be enabled for email sending. 我一直在网上阅读有关可能未启用代理服务器进行电子邮件发送的可能性。 Please correct me if I read or understood wrong? 如果我阅读或理解错误,请纠正我? As this proxy allows me to login for gmail account when i use browser. 由于此代理允许我使用浏览器时登录Gmail帐户。
Any help will be appreciated. 任何帮助将不胜感激。
Regards 问候
// Use the function below it will work behind proxy even //使用下面的函数甚至可以在代理后面工作
using System.Net.Mail;
public int SendMailUsingGMAIL(string fromAddress, string toAddress, string tocc, string mailsubject, string msgContent, string strAttachment, bool isBodyHTML)
{
int retvar = 0;
try
{
MailMessage mailMessage = new MailMessage(new MailAddress(fromAddress)
, new MailAddress(toAddress));
mailMessage.Subject = mailsubject;
mailMessage.IsBodyHtml = isBodyHTML;
mailMessage.Body = msgContent;
if (tocc != "")
{
mailMessage.CC.Add(tocc);
}
System.Net.NetworkCredential networkCredentials = new
System.Net.NetworkCredential("smtpUserName", "smtpPassword");//key="smtpUserName" value="urid@gmail.com";key="smtpPassword" value="your password"
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredentials;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
retvar = -1;
}
return retvar;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.