簡體   English   中英

點擊按鈕發送郵件

[英]send mail on button click

我正在嘗試單擊按鈕發送郵件。 在本地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;
        }
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM