使用CDO通过代理发送电子邮件
本文关键字:电子邮件 代理 CDO 使用 | 更新日期: 2023-09-27 18:23:59
我想从C#winform应用程序发送电子邮件,我的互联网连接使用代理。
这就是我到目前为止所做的
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);
}
它引发异常,传输无法连接到服务器。我已经尝试了465587和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="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;
}