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

时间:2012-02-09 14:57:42

标签: c# winforms email

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

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

我一直在网上阅读有关代理服务器无法启用电子邮件发送的可能性。如果我阅读或理解错误,请纠正我?由于此代理允许我在使用浏览器时登录gmail帐户。

任何帮助将不胜感激。

此致

1 个答案:

答案 0 :(得分:1)

//使用下面的功能它甚至可以在代理后面工作

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;

}