none
the requested name is valid but no data of the requested type was found

    Question

  • using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using aspNetEmail;
    using System.Threading;
    namespace wincs
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    #region Editor Generated
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.RadioButton optCallback;
    private System.Windows.Forms.RadioButton optWaitOne;
    private System.Windows.Forms.RadioButton optPoll;
    private System.Windows.Forms.TextBox txtResults;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button cmdSend;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.cmdSend = new System.Windows.Forms.Button();
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.optPoll = new System.Windows.Forms.RadioButton();
    this.optWaitOne = new System.Windows.Forms.RadioButton();
    this.optCallback = new System.Windows.Forms.RadioButton();
    this.txtResults = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.groupBox1.SuspendLayout();
    this.SuspendLayout();
    // 
    // cmdSend
    // 
    this.cmdSend.Location = new System.Drawing.Point(16, 32);
    this.cmdSend.Name = "cmdSend";
    this.cmdSend.TabIndex = 0;
    this.cmdSend.Text = "Send Email";
    this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.optPoll,
    this.optWaitOne,
    this.optCallback});
    this.groupBox1.Location = new System.Drawing.Point(16, 72);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(224, 128);
    this.groupBox1.TabIndex = 1;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "Async Send Options";
    // 
    // optPoll
    // 
    this.optPoll.Location = new System.Drawing.Point(16, 80);
    this.optPoll.Name = "optPoll";
    this.optPoll.Size = new System.Drawing.Size(152, 24);
    this.optPoll.TabIndex = 2;
    this.optPoll.Text = "Send Using Polling";
    // 
    // optWaitOne
    // 
    this.optWaitOne.Location = new System.Drawing.Point(16, 52);
    this.optWaitOne.Name = "optWaitOne";
    this.optWaitOne.Size = new System.Drawing.Size(152, 24);
    this.optWaitOne.TabIndex = 1;
    this.optWaitOne.Text = "Send Using WaitOne()";
    // 
    // optCallback
    // 
    this.optCallback.Checked = true;
    this.optCallback.Location = new System.Drawing.Point(16, 24);
    this.optCallback.Name = "optCallback";
    this.optCallback.Size = new System.Drawing.Size(128, 24);
    this.optCallback.TabIndex = 0;
    this.optCallback.TabStop = true;
    this.optCallback.Text = "Send Using Callback";
    // 
    // txtResults
    // 
    this.txtResults.Location = new System.Drawing.Point(16, 232);
    this.txtResults.Multiline = true;
    this.txtResults.Name = "txtResults";
    this.txtResults.Size = new System.Drawing.Size(224, 112);
    this.txtResults.TabIndex = 2;
    this.txtResults.Text = "";
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(16, 216);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(42, 13);
    this.label1.TabIndex = 3;
    this.label1.Text = "Results";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(280, 357);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
     this.label1,
     this.txtResults,
     this.groupBox1,
     this.cmdSend});
    this.Name = "Form1";
    this.Text = "Sending Email Async";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    #endregion
    /// <summary>
    /// The email message object
    /// </summary>
    private EmailMessage msg = new EmailMessage();
    /// <summary>
    /// Inits the msg object
    /// </summary>
    private void InitEmail()
    {
                msg.Server = "127.0.0.1";
    msg.FromAddress = "me@mycompany.com";
    msg.To = "you@yourcompany.com";
    msg.Subject = "This email sent async";
    msg.Body = "this is where the body goes";
    }
    /// <summary>
    /// Determines which Async option to use
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void cmdSend_Click(object sender, System.EventArgs e)
    {
    //clear the results
    txtResults.Text = string.Empty;
    //init the emailmessage object
    InitEmail();
    //check to see which option is selected
    if( optCallback.Checked)
    SendUsingCallback();
    else if( optWaitOne.Checked )
    SendUsingWaitOne();
    else
    SendUsingPolling();
    }
    #region Callback Technique
    /// <summary>
    /// Async send using the Callback technique
    /// </summary>
    private void SendUsingCallback()
    {
    //register the callback, use the msg object as state, and start the send
    msg.BeginSend( new AsyncCallback( MyCallback ), msg );
    //process immedately resumes to the calling thread
    }
    /// <summary>
    /// The callback called by the BeginSend method
    /// This method called after the send has completed.
    /// </summary>
    /// <param name="result"></param>
    /// <returns></returns>
    private void MyCallback( IAsyncResult result )
    {
    bool Success = true;
    try
    {
    //successful send
    Success = msg.EndSend( result );
    txtResults.Text = "The email was sent.";
    }
    catch(Exception ex )
    {
    // write any exceptions out to the textbox
    txtResults.Text = "The email failed, here is the exception: " + ex.Message;
    Success = false;
    }
    }
    #endregion
    #region WaitOne()
    /// <summary>
    /// Send async and call wait one. WaitOne waits for the async process to complete before resuming
    /// </summary>
    private void SendUsingWaitOne()
    {
    //start the send
    IAsyncResult result = msg.BeginSend(null, null );
    //processing immediately resumes
    //here we would do a bunch of work on the main thread
    //now check to see if we still have to wait for the async process to comlete
    result.AsyncWaitHandle.WaitOne();
    //check our results
    bool Success = true;
    try
    {
    Success  = msg.EndSend( result );
    txtResults.Text = "The email was sent.";
    }
    catch(Exception ex )
    {
    txtResults.Text = "The email was not sent, here is the exception: " + ex.Message;
    }
    }
    #endregion
    #region Polling
    /// <summary>
    /// Polling allows us to poll for completion
    /// </summary>
    private void SendUsingPolling()
    {
    //start the send
    IAsyncResult result = msg.BeginSend( null, null );
    //processing immediately resumes
    //here we would do a bunch of work on the main thread
    //now we poll until the background async process has completed
    if( !result.IsCompleted)
    Thread.Sleep( 100 ); //sleep for 100ms
    //check our results
    bool Success = true;
    try
    {
    Success  = msg.EndSend( result );
    txtResults.Text = "The email was sent.";
    }
    catch(Exception ex )
    {
    txtResults.Text = "The email was not sent, here is the exception: " + ex.Message;
    }
    }
    #endregion
    private void Form1_Load(object sender, System.EventArgs e)
    {
    }
    }
    }
    i download this example from web when i try this it showing me an error: "the requested name is valid but no data of the requested type was found"
    Tuesday, September 20, 2011 9:29 AM

Answers

  • Thanks Riced for your help.

     

    Hi Hitesh,

    Welcome to the MSDN forum!

     

    MailMessage Class (System.Net.Mail)

    According to this code, the EmailMessage should be a custom/derived class where some function implementation has been encapsulated inside.

    using aspNetEmail;

    private EmailMessage msg = new EmailMessage();

     

    You’ll need to contact the author to get the detailed explanation. This issue is likely to be related at Mail SMTP server configuration.

     

    There is a case discussing the similar issue for you to check.

    “The requested name is valid but no data of the requested type was found

    http://forums.incredimail.com/viewtopic.php?f=10&t=115&start=0

     

    Code sample: How to use System.Net.Mail.MailMessage object to send email in C#.

    using System.Net.Mail;
    public class 
    Form1 {
        
    private void Button1_Click(object sender, System.EventArgs e) {
            MailMessage mailInstance 
    = new MailMessage("FromMailAdress""ToMailAdress");
            
    // mailInstance.To = ToMailAdress1; ToMailAdress2
            // mailInstance.From = FromMailAdress
            
    mailInstance.Subject "This is mail subject";
            
    mailInstance.Body "This is mail body content";
            
    mailInstance.Attachments.Add(new Attachment("filename"));
            
    // Optional
            
    SmtpClient mailSenderInstance = new SmtpClient("smtpHostAdress"25);
            
    // 25 is the port of the SMTP host
            
    mailSenderInstance.Credentials = new System.Net.NetworkCredential("LoginAccout""Password");
            
    mailSenderInstance.Send(mailInstance);
            
    mailInstance.Dispose();
            
    // Please remember to dispose this object
        
    }
    }

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 9:29 AM

All replies

  • At a guess the email addresses you are using are not valid. Try changing them to your own.

    It's a guess because (a) it is difficult to read 280 lines of unformatted code - you only need to show the code where the error occurs, (b) I have to guess which line creates the error.


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Tuesday, September 20, 2011 11:14 AM
  • for security i change mail address 

    in this code i am not getting any error

    but when i send any mail it will go below line

     

    catch(Exception ex )
     {
     txtResults.Text = "The email was not sent, here is the exception: " + ex.Message;
    and ex.message is showing me this error: "the requested name is valid but no data of the requested type was found"

     

    Tuesday, September 20, 2011 11:27 AM
  • So now we know it is one of two catch blocks that trigger the message. But which one?
    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Tuesday, September 20, 2011 1:04 PM
  • in my code i put 3 way to send mail

    i try every option but every

    but every option throw to error

    Wednesday, September 21, 2011 5:54 AM
  • Thanks Riced for your help.

     

    Hi Hitesh,

    Welcome to the MSDN forum!

     

    MailMessage Class (System.Net.Mail)

    According to this code, the EmailMessage should be a custom/derived class where some function implementation has been encapsulated inside.

    using aspNetEmail;

    private EmailMessage msg = new EmailMessage();

     

    You’ll need to contact the author to get the detailed explanation. This issue is likely to be related at Mail SMTP server configuration.

     

    There is a case discussing the similar issue for you to check.

    “The requested name is valid but no data of the requested type was found

    http://forums.incredimail.com/viewtopic.php?f=10&t=115&start=0

     

    Code sample: How to use System.Net.Mail.MailMessage object to send email in C#.

    using System.Net.Mail;
    public class 
    Form1 {
        
    private void Button1_Click(object sender, System.EventArgs e) {
            MailMessage mailInstance 
    = new MailMessage("FromMailAdress""ToMailAdress");
            
    // mailInstance.To = ToMailAdress1; ToMailAdress2
            // mailInstance.From = FromMailAdress
            
    mailInstance.Subject "This is mail subject";
            
    mailInstance.Body "This is mail body content";
            
    mailInstance.Attachments.Add(new Attachment("filename"));
            
    // Optional
            
    SmtpClient mailSenderInstance = new SmtpClient("smtpHostAdress"25);
            
    // 25 is the port of the SMTP host
            
    mailSenderInstance.Credentials = new System.Net.NetworkCredential("LoginAccout""Password");
            
    mailSenderInstance.Send(mailInstance);
            
    mailInstance.Dispose();
            
    // Please remember to dispose this object
        
    }
    }

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 9:29 AM