Visual studio 使用C语言通过Visual Studio 2008发送电子邮件#

Visual studio 使用C语言通过Visual Studio 2008发送电子邮件#,visual-studio,c#-3.0,Visual Studio,C# 3.0,我正在将Visual Studio 2008 Express与C#一起使用。我一直在尝试让一个电子邮件例程在aspx页面的代码隐藏中工作 String strPassed = ""; System.Web.Mail.MailMessage String strEmailTo = "touser@xyz.org"; String strEmailFrom = "fromuser@xyz.org"; String st

我正在将Visual Studio 2008 Express与C#一起使用。我一直在尝试让一个电子邮件例程在aspx页面的代码隐藏中工作

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
所有MSDN示例,即使是那些声明用于.NET3.5的示例,都不会编译。MailMessage类显然已经更改了几次。下面是确实可以编译的代码,但这行代码SmtpMail.Send(msg)有一条模糊的错误消息:

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
与“System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)”匹配的最佳重载方法包含一些无效参数

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
有人能看到无效的争论可能是什么吗?这就是阻止这一切起作用的原因

using System.Net;
using System.Net.Mail;

MailMessage msg = new MailMessage();

msg.ToAddress = new MailAddress("someone@yourCompany.com");
msg.FromAddress = ("me@myCompany.com");
msg.CCAddress = ("boss@myCompany.com");

msg.EmailMessage = "Order message test";
msg.EmailSubject = "Order Confirmation";
msg.MailEncoding = "html";
msg.MailPriority = MailPriority.Normal.ToString();

SmtpClient SmtpMail = new SmtpClient();

SmtpMail.Host = "smtpout.secureserver.net";
SmtpMail.Port = 25;

try
{
    SmtpMail.Send(msg);    // This is where the error occurs.
}
catch (Exception ex)
{
    //  Incomplete here
}
        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;

看起来您正在为使用自定义包装。这些属性都不是.NET类的成员(对于我能找到的任何版本)

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
对于3.5,您将使用:

msg.To.Add(new MailAddress("someone@yourCompany.com"));
msg.From.Add(new MailAddress("me@myCompany.com"));
msg.CC.Add(new MailAddress("boss@myCompany.com"));

msg.Body = "Order message test";
msg.Subject = "Order Confirmation";
msg.BodyEncoding = Encoding.Default;
msg.Priority = MailPriority.Normal;
        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;

看起来您正在尝试做一些非常复杂的事情;我会先尝试一个更简单的示例,然后逐步升级。该网站有很多您可能会发现有用的资源。这是:

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
下面的示例使用C#.net 3.0框架

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
以下是我使用的额外库:

        using System.Net.Mail;  //.MailMessage
        using Microsoft.VisualBasic; //imports control characters for string formatting
        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
以下是我的电子邮件功能:

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;
String strPassed=“”;
System.Web.Mail.MailMessage
字符串strEmailTo=”touser@xyz.org";
字符串strEmailFrom=”fromuser@xyz.org";
字符串strEmailBCC=“”;
字符串主题;
字符串strEmailBody=“”;
strEmailSubject=“注意用户-请求电子邮件”;
strEmailBody=“您请求了xyz站点的密码

“+ “账户信息…
”; strEmailBody=strMessage; strEmailTo=strEmail; System.Net.Mail.MailMessage objMessage=新系统.Net.Mail.MailMessage(strEmailFrom,strEmailTo); objMessage.IsBodyHtml=true; objectmessage.Subject=strEmailSubject; objMessage.Body=strEmailBody;//emailbody; MailAddressCollection objAddress=新的MailAddressCollection(); System.Net.Mail.SmtpClient objClientPath=new System.Net.Mail.SmtpClient(); SmtpClient smtp=新SmtpClient(); 尝试 { strPassed=“已通过:通过电子邮件发送”; objClientPath.Host=“mailhost.xyz.org”; 发送(objMessage); } 捕获(System.Exception错误) { strPassed=“failed:”+err.ToString();/“,如果出现错误 } 返回strPassed;
此代码的任何内容都不正确。MailMessage类没有您在代码中编写的任何属性。