Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击按钮发送邮件_C#_Asp.net - Fatal编程技术网

C# 单击按钮发送邮件

C# 单击按钮发送邮件,c#,asp.net,C#,Asp.net,我正试图通过点击按钮发送邮件。在本地pc上,代码正在工作,但在服务器上,代码不工作。它给出错误“无法连接到远程服务器源:-SystemData:-System.Collections.ListDictionaryInternal”请帮助 home.aspx public partial class About : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { }

我正试图通过点击按钮发送邮件。在本地pc上,代码正在工作,但在服务器上,代码不工作。它给出错误“无法连接到远程服务器源:-SystemData:-System.Collections.ListDictionaryInternal”请帮助

home.aspx

public partial class About : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SendContactUsEmail();
        SendAutoResponseEmail();

    }
    private bool SendContactUsEmail()
    {
        MailMessage MailObject = new MailMessage();

        MailObject.From = new MailAddress(ConfigurationSettings.AppSettings["EmailFrom"].ToString());
        MailObject.To.Add(new MailAddress("test@technosys.com"));
        MailObject.Subject = " technosys Contact Alert Subject : ";
        string ErrorHandling_Message = "Dear Manager," + " " + "<br/>" + "A customer want to contact you with following details:";
        ErrorHandling_Message += "<br><br>Name: vesh";
        ErrorHandling_Message += "<br><br>Email: test@technosys";
        ErrorHandling_Message += "<br><br>Company: technosys";
        ErrorHandling_Message += "<br><br>Phone: 123456789";
        ErrorHandling_Message += "<br><br>Message: Test mail on live server";
        ErrorHandling_Message += "<br><br>Sincerely" + "<br>" + "Web Controller" + "<br>" + "<b>Technosys</b>";
        try
        {
            MailObject.Body = ErrorHandling_Message;
            MailObject.IsBodyHtml = true;
            MailObject.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["EmailFrom"].ToString(), ConfigurationSettings.AppSettings["Password"].ToString());
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(MailObject);
            Label1.Text = "Done";
            return true;
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
            return false;
        }
    }
    private bool SendAutoResponseEmail()
    {
        MailMessage MailObject = new MailMessage();

        MailObject.From = new MailAddress(ConfigurationSettings.AppSettings["EmailFrom"].ToString());
        MailObject.To.Add(new MailAddress("test@technosys.com"));
        MailObject.Subject = "Thanks for your request at technosys";
        string ErrorHandling_Message = "Dear, " + "<br/></br>" + "Thanks for your email. You can expect to hear back from us with an answer to your inquiry within : 24 hours.";
        ErrorHandling_Message += "<br><br>We look forward to helping you.";
        ErrorHandling_Message += "<br><br>Sincerely" + "<br>" + "<br>" + "<b>Technosys</b>" + "<br><img alt=\"\"  src=\"http:/images/logomain.jpg\" >";
        try
        {
            MailObject.Body = ErrorHandling_Message;
            MailObject.IsBodyHtml = true;
            MailObject.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["EmailFrom"].ToString(), ConfigurationSettings.AppSettings["Password"].ToString());
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(MailObject);
            Label1.Text = "Done";
            return true;
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
            return false;
        }
    }
}
public分部类关于:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
SendContactUsEmail();
SendAutoResponseEmail();
}
私人bool SendContactUsEmail()
{
MailMessage MailObject=新邮件();
MailObject.From=新邮件地址(ConfigurationSettings.AppSettings[“EmailFrom”].ToString());
MailObject.To.Add(新邮件地址(“test@technosys.com"));
MailObject.Subject=“technosys联系人警报主题:”;
string ErrorHandling_Message=“亲爱的经理,”+“+”
“+”一位客户希望与您联系,并提供以下详细信息:”; ErrorHandling_Message+=“

名称:vesh”; 错误处理\u消息+=“

电子邮件:test@technosys"; ErrorHandling_Message+=“

公司:technosys”; 错误处理信息+=“

电话:123456789”; ErrorHandling_Message+=“

Message:在live server上测试邮件”; 错误处理\u消息+=“

真诚”+“
”+“Web控制器”+“
”+“Technosys”; 尝试 { MailObject.Body=ErrorHandling\u消息; MailObject.IsBodyHtml=true; MailObject.Priority=MailPriority.High; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.gmail.com”; smtp.Port=587; smtp.Credentials=新网络凭据(ConfigurationSettings.AppSettings[“EmailFrom”].ToString(),ConfigurationSettings.AppSettings[“Password”].ToString()); smtp.EnableSsl=true; smtp.DeliveryMethod=SmtpDeliveryMethod.Network; smtp.Send(邮件对象); 标签1.Text=“完成”; 返回true; } 捕获(例外情况除外) { Label1.Text=例如消息; 返回false; } } 私人bool SendAutoResponseEmail() { MailMessage MailObject=新邮件(); MailObject.From=新邮件地址(ConfigurationSettings.AppSettings[“EmailFrom”].ToString()); MailObject.To.Add(新邮件地址(“test@technosys.com")); MailObject.Subject=“感谢您在technosys的请求”; string ErrorHandling_Message=“亲爱的,”+“

“+”感谢您的电子邮件。您可以在24小时内收到我们的回复,并对您的询问作出答复。”; ErrorHandling_Message+=“

我们期待着为您提供帮助。”; 错误处理信息+=“

真诚”+“
”+“
”+“+”
“+”Technosys“+”
”; 尝试 { MailObject.Body=ErrorHandling\u消息; MailObject.IsBodyHtml=true; MailObject.Priority=MailPriority.High; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.gmail.com”; smtp.Port=587; smtp.Credentials=新网络凭据(ConfigurationSettings.AppSettings[“EmailFrom”].ToString(),ConfigurationSettings.AppSettings[“Password”].ToString()); smtp.EnableSsl=true; smtp.DeliveryMethod=SmtpDeliveryMethod.Network; smtp.Send(邮件对象); 标签1.Text=“完成”; 返回true; } 捕获(例外情况除外) { Label1.Text=例如消息; 返回false; } } }
web.config

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>
    <appSettings>
        <add key="EmailFrom" value="test@technosys.com"/>
      <add key="Password" value="best"/>
    </appSettings>
    <connectionStrings/>
    <system.web>
    <customErrors  mode="Off">

    </customErrors>
 <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
        <compilation debug="true" targetFramework="4.0">
        </compilation>
        <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
        <authentication mode="Windows"/>
        <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
    <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
</configuration>

为什么不直接使用这个:

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587);
smtpClient.EnableSsl = true;
System.Net.NetworkCredential credentials = new 
                   System.Net.NetworkCredential(
                       ConfigurationSettings.AppSettings["EmailFrom"].ToString(),
                       ConfigurationSettings.AppSettings["Password"].ToString()
                   );
smtpClient.Credentials = credentials;

string[] tos;
this.to=this.to.Replace(',',' ');
//this.Bcc = this.bcc;
tos=this.to.Split();
for (int i = 0; i < tos.Length; i++)
{
    MailMessage ml = new MailMessage(this.from, tos[i].ToString(), this.sub, 
                                     this.msg);
    ml.IsBodyHtml = true;
    smtpClient.Send(ml);
}
SmtpClient-SmtpClient=newsmtpclient(“smtp.gmail.com”,587);
smtpClient.EnableSsl=true;
System.Net.NetworkCredential凭据=新建
System.Net.NetworkCredential(
ConfigurationSettings.AppSettings[“EmailFrom”].ToString(),
ConfigurationSettings.AppSettings[“密码”].ToString()
);
smtpClient.Credentials=凭证;
字符串[]tos;
this.to=this.to.Replace(',','');
//this.Bcc=this.Bcc;
tos=此.to.Split();
for(int i=0;i
哪一行代码产生错误?显示完整的堆栈跟踪。行我找不到,但使用标签我得到这个错误“无法连接到远程服务器”。这段代码在本地运行得很好,但在我的托管服务器上上传后就不起作用了。作为一个完整的补充说明,您可以通过将ErrorHandling_消息字符串更改为StringBuilder来提高性能。字符串实际上并没有连接,它们会随着每次更改而被销毁和重建。这对你的问题没有帮助,只是一个想法:)你可以通过不显式地丢弃异常信息来获得堆栈跟踪。除了消息之外,异常还有很多。您可能希望研究一个更健壮的错误日志记录系统(我通常更喜欢Log4Net),而不只是在应用程序中显示字符串。当出现错误时,你可以收集有用的信息。我每天通过我们的应用程序通过gmail发送电子邮件-我们不设置端口。你评论过并尝试过吗?此外,在您的生产服务器上,端口587是否处于打开状态?
to、from、sub、msg等。
是变量。。您可以在同一页面中声明…@user1724912