使用CDO通过代理发送电子邮件
问题描述:
我想从C#winform应用程序发送电子邮件,我的Internet连接使用代理。使用CDO通过代理发送电子邮件
这是我迄今所做
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);
}
它抛出一个异常,运输未能连接到服务器。 我已经尝试过465,587和25端口的Gmail的SMTP服务器。什么都没有
我一直在通过网络阅读有关代理服务器可能无法启用电子邮件发送的可能性。如果我阅读或理解错误,请纠正我的错误?由于此代理允许我在使用浏览器时登录gmail帐户。
任何帮助将不胜感激。
问候
答
//使用了以下功能,将背后的代理工作,即使
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="[email protected]";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;
}
+0
nopes,它不适合我的工作:( – nightWatcher 2012-02-14 10:38:02
+0
什么异常你得到PLZ份额! – Zia 2012-02-15 15:04:01
我没想到网络代理通常支持的非HTTP通信。你确定你不想在这里使用SOCKS代理吗? – 2012-02-09 15:06:41
@ M.Babcock任何指向该教程的链接?我怎样才能做到这一点? – nightWatcher 2012-02-09 15:11:35
可能的重复[是否有.NET库通过PROXY发送电子邮件?](http://*.com/questions/3156753/is-there-net-library-for-email-sending-via-proxy) – 2012-02-09 15:16:26