PDA

View Full Version : System.Reflection.TargetInvocationException



faramarz_s
شنبه 19 مهر 1382, 05:34 صبح
اصل برنامه ساده و کاملا مشخص است:::ارسال نامه به فردی از طریق وبفرمها

using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace sendMail
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label lblResult;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.RegularExpressionValidat or RegularExpressionValidator1;
protected System.Web.UI.WebControls.TextBox TextBox2;


public string getConnectionStrFromWebConfig(string webConfigKey_ConnectionStr)
{
// the best way for getting state from web.config
return System.Configuration.ConfigurationSettings.AppSett ings[webConfigKey_ConnectionStr];
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
MailMessage Mailer = new MailMessage();
Mailer.From = TextBox1.Text;
Mailer.To = "faramarz_s@hotmail.com";
Mailer.Subject = TextBox2.Text;
Mailer.Body = TextBox3.Text;
Mailer.BodyFormat = System.Web.Mail.MailFormat.Html;
SmtpMail.Send(Mailer);
lblResult.Visible=true;
lblResult.Text="نامه شما با موفقیت ارسال گردید";
}
catch(Exception ex)
{
lblResult.Visible=true;
lblResult.Text = "خطا در ارسال نامه! " + ex.ToString();
}
}
}
}


نتیجه امتحان برنامه بر روی هاست:

خطا در ارسال نامه! System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80040220): The "SendUsing" configuration value is invalid. --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) at System.Web.Mail.LateBoundAccessHelper.CallMethod(O bject obj, String methodName, Object[] args) --- End of inner exception stack trace --- at System.Web.Mail.LateBoundAccessHelper.CallMethod(O bject obj, String methodName, Object[] args) at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at sendMail.WebForm1.Button1_Click(Object sender, EventArgs e) in d:\inetpub\wwwroot\projects\sendmail\default.aspx. cs:line 80

Vahid_Nasiri
شنبه 19 مهر 1382, 21:04 عصر
چرا smptMailserver را مشخص نکرده اید؟

faramarz_s
یک شنبه 20 مهر 1382, 22:19 عصر
1-مگر بطور دیفالت مقدار دهی نمیشود؟
2-در ضمن سورس فوق عین MSDNمی باشد و تعجب می کنم ایراد داشته باشد.
3- در هر صورت جمله یا جملاتی را که باید بیافزایم را بفرمایید.
با تشکر

Vahid_Nasiri
دوشنبه 21 مهر 1382, 20:21 عصر
این را تست کنید و نتیجه را اعلام بفرمایید:


private void Button1_Click(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage();

mail.From = "gabor.erdi@sap.com";
mail.To = "gabor.erdi@sap.com";
mail.Subject = "New ad-hoc request was posted";
mail.Body = "There is new ad-hoc request";
mail.BodyFormat = MailFormat.Text;

SmtpMail.SmtpServer = "localhost";

SmtpMail.Send(mail);

}

Vahid_Nasiri
دوشنبه 21 مهر 1382, 20:23 عصر
نکته اش این است:



I had a similar problem - had to add SMTPServer = "localhost"
and go to IIS > SMTP Server > right click Properties > Access tab > Click the Relay button > Add > in the IP address 127.0.0.1 - this is localhost.

مرجع :
http://p2p.wrox.com/topic.asp?TOPIC_ID=4959

faramarz_s
دوشنبه 21 مهر 1382, 22:36 عصر
جناب نصیری؛
اصولا SMTP SERVER را در کلاینت نصب نکرده ام اما برنامه را روی هاست وب ماتریکس امتحان نموده و دقیقا همین پیغم را میدهد!
آنجا که دیگر لوکال هاست معنی ندارد!
در ضمن عین این برنامه قبلا جواب می داد!!!
با اینحال شما بفرمایید...

faramarz_s
سه شنبه 22 مهر 1382, 07:17 صبح
The transport failed to connect to the server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: The transport failed to connect to the server.

Source Error:


Line 104: SmtpMail.SmtpServer = "localhost";
Line 105:
Line 106: SmtpMail.Send(mail);
Line 107: }
Line 108: }