none
How to send mail In asp.net using SMTP settings? with out web.config entry

    Question

  • Hi to all,

    I want to send mail using SMTP settings. And it will not be configured in web.config. I want to set the SMTP settings at run time. How I can do this?

    It(SMTP settings) may be saved in Database or file.

    Thanks,

    Naga Harish .

    Friday, March 26, 2010 5:19 AM

Answers

  • use like this:

    System.Net.

    NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(UserName, Password);//Gmail username and password

    MailMessage mail = new MailMessage(UserName, txtMailadd.Text, "message");

     

    SmtpClient smclient = new SmtpClient();

    smclient.Host = ServerName; //server name like smtp.gmail.com

    smclient.Port = 587;

    smclient.EnableSsl =

    true;

    smclient.UseDefaultCredentials =

    false;

    smclient.Credentials = basicAuthenticationInfo;

    smclient.Send(mail);


    Varun Kumar
    • Proposed as answer by varun007 Monday, March 29, 2010 6:38 AM
    • Marked as answer by SamAgain Wednesday, March 31, 2010 11:24 AM
    Monday, March 29, 2010 6:38 AM
  • To use custom credentials:

    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("username", "password");

    • Proposed as answer by M.L. Somers Monday, March 29, 2010 9:19 AM
    • Marked as answer by SamAgain Wednesday, March 31, 2010 11:23 AM
    Monday, March 29, 2010 9:19 AM

All replies

  • Save SMTP Settings in database and fetch them on your form using select statement and get different setting values in different variables and pass them one by one in a mail function. Please ask if u want a example.


    Varun Kumar
    • Proposed as answer by varun007 Friday, March 26, 2010 5:25 AM
    Friday, March 26, 2010 5:25 AM
  • Hi,

    hope it helps

    MailMessage message = new MailMessage();
                MailAddress add=new MailAddress(TextBox1.Text.ToString(),"name");
                message.From = add;
                message.To.Add(TextBox2.Text.ToString());
                message.Subject = TextBox4.Text.ToString();
                message.Body = TextBox3.Text.ToString();
                SmtpClient client = new SmtpClient("SMTPclient");
                client.Send(message);

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    Friday, March 26, 2010 5:49 AM
  • Hey Varun Kumar,

    I can do those thing. But Please check with my Error msg. I am getting that msg when I used other domain mail Id. some thing like gmail. It may be from or to or Both. It showing that error.

     

    Thanks,

    Naga Harish .

    Friday, March 26, 2010 7:05 AM
  • use like this:

    System.Net.

    NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(UserName, Password);//Gmail username and password

    MailMessage mail = new MailMessage(UserName, txtMailadd.Text, "message");

     

    SmtpClient smclient = new SmtpClient();

    smclient.Host = ServerName; //server name like smtp.gmail.com

    smclient.Port = 587;

    smclient.EnableSsl =

    true;

    smclient.UseDefaultCredentials =

    false;

    smclient.Credentials = basicAuthenticationInfo;

    smclient.Send(mail);


    Varun Kumar
    • Proposed as answer by varun007 Monday, March 29, 2010 6:38 AM
    • Marked as answer by SamAgain Wednesday, March 31, 2010 11:24 AM
    Monday, March 29, 2010 6:38 AM
  • To use custom credentials:

    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("username", "password");

    • Proposed as answer by M.L. Somers Monday, March 29, 2010 9:19 AM
    • Marked as answer by SamAgain Wednesday, March 31, 2010 11:23 AM
    Monday, March 29, 2010 9:19 AM
  • Hi to all,

    I want to send mail using SMTP settings. And it will not be configured in web.config. I want to set the SMTP settings at run time. How I can do this?

    It(SMTP settings) may be saved in Database or file.

    Thanks,

    Naga Harish .

     

    // Create a new instance of the SmtpClient class.
    SmtpClient client =  new  SmtpClient();
    // Connect to the server.
    client.Connect( "myserver" );
    // Or you can specify the SMTP port with
    // client.Connect("myserver", 25);
    // Login to the server.
    client.Authenticate( "user" ,  "password" );
    // Create a new mail message.
    MailMessage msg =  new  MailMessage();
    msg.Subject =
     "Test Subject" ;
    msg.BodyText =
     "Content" ;
    msg.From =
     "from@mydomain.com " ;
    msg.To =
     "to@somedomain.com " ;
    // And send it.
    client.Send(msg);
    // Close the connection.
    client.Disconnect();

     

    Information about these libraries can also be found at this SMTP web blog , imapblog , pop3blog , and this Mail web blog .

    • Edited by CaddreModerator Friday, May 21, 2010 2:38 PM Product Ad removed
    Sunday, May 09, 2010 4:15 AM
  • A way of sending email in asp.net 3.5, working code:

    here:

    http://fareed-aspx-blog.blogspot.com/2010/11/send-mail-in-aspnet.html

    Saturday, November 06, 2010 2:41 PM
  • What will be the source code in case of a yahoo email-id ? What is the port number for yahoo ?
    Monday, April 04, 2011 3:37 PM
  • The above should work with Yahoo, see: http://help.yahoo.com/l/us/yahoo/smallbusiness/bizmail/pop/pop-03.html

    Monday, April 04, 2011 9:07 PM
  • Thanks a lot sir :)
    Thursday, April 07, 2011 5:22 PM