按钮点击发送邮件

时间:2012-11-02 13:18:40

标签: c# asp.net

我正在尝试按下按钮发送邮件。在本地PC上代码正在运行,但在服务器上代码不起作用。它给出错误“无法连接到远程serverSource:-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;
        }
    }
}

的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>

1 个答案:

答案 0 :(得分:0)

你为什么不用这个:

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);
}