gpt4 book ai didi

c# - 使用 CDO 通过代理发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:20 26 4
gpt4 key购买 nike

我想从 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);
}

它抛出异常,传输无法连接到服务器。我已经为 Gmail smtp 服务器的端口尝试了 465,587 和 25。什么都没用。

我一直在网上阅读有关代理服务器可能未启用电子邮件发送的可能性的信息。如果我阅读或理解错误,请纠正我?因为这个代理允许我在使用浏览器时登录 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;

}

关于c# - 使用 CDO 通过代理发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9213155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com