locked
Sending email through Web-Service (asmx) RRS feed

  • Question

  • User1899595498 posted

    Do you have an example how to setup sending email through Web-Service (asmx) using C# with ASP.NET 3.5?

    Also please show the “web.config” file for mailSettings setup. Thanks.

    Saturday, June 23, 2012 7:46 PM

Answers

  • User1320101480 posted

    Do you know how to do an asmx web service and call it?

    If you do then just write a piece of code inside a web method which sends the mail.Will post a sample mail sending code here.

    This sample code uses Gmail's SMTP server to send emails, if you want to send using your own SMTP server then just chalne the smtp settings or else use the same code and chane the Username /password

    System.Net.Mail; //Include This NameSpace
    
    MailMessage MyMailMessage = new MailMessage();
    MyMailMessage.From = new MailAddress("adbuaryuy@gmail.com");
    MyMailMessage.To.Add("yearningpeeks@yahoo.com");
    MyMailMessage.Subject = "Feedback Form";
    MyMailMessage.IsBodyHtml = true;
    
    MyMailMessage.Body = "<table><tr><td>" + txtName.Text + txtEmail.Text + txtComments.Text + "</table></tr></td>";
    
    SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
    SMTPServer.Port = 587;
    SMTPServer.Credentials = new System.Net.NetworkCredential("adbuaryuy@gmail.com", System.Configuration.ConfigurationSettings.AppSettings["pwd"].ToString());
    SMTPServer.EnableSsl = true;
    try
    {
    SMTPServer.Send(MyMailMessage);
    Response.Redirect("Thankyou.aspx");
    }
    catch (Exception ex)
    {
    
    
    }
    
    http://www.aspsnippets.com/Articles/Create-and-send-HTML-Formatted-Emails-in-ASP.Net-using-C-and-VB.Net.aspx
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 24, 2012 2:11 AM
  • User1320101480 posted

    Yes you can use it.With this code, you don't have to put any other details in web.config.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 25, 2012 12:11 PM

All replies

  • User1320101480 posted

    Do you know how to do an asmx web service and call it?

    If you do then just write a piece of code inside a web method which sends the mail.Will post a sample mail sending code here.

    This sample code uses Gmail's SMTP server to send emails, if you want to send using your own SMTP server then just chalne the smtp settings or else use the same code and chane the Username /password

    System.Net.Mail; //Include This NameSpace
    
    MailMessage MyMailMessage = new MailMessage();
    MyMailMessage.From = new MailAddress("adbuaryuy@gmail.com");
    MyMailMessage.To.Add("yearningpeeks@yahoo.com");
    MyMailMessage.Subject = "Feedback Form";
    MyMailMessage.IsBodyHtml = true;
    
    MyMailMessage.Body = "<table><tr><td>" + txtName.Text + txtEmail.Text + txtComments.Text + "</table></tr></td>";
    
    SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
    SMTPServer.Port = 587;
    SMTPServer.Credentials = new System.Net.NetworkCredential("adbuaryuy@gmail.com", System.Configuration.ConfigurationSettings.AppSettings["pwd"].ToString());
    SMTPServer.EnableSsl = true;
    try
    {
    SMTPServer.Send(MyMailMessage);
    Response.Redirect("Thankyou.aspx");
    }
    catch (Exception ex)
    {
    
    
    }
    
    http://www.aspsnippets.com/Articles/Create-and-send-HTML-Formatted-Emails-in-ASP.Net-using-C-and-VB.Net.aspx
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 24, 2012 2:11 AM
  • User1899595498 posted

    Can I use my free of charge gmail to send the email is like your example?

    Can you show the setting "web.config" file?

    Thanks.

    Sunday, June 24, 2012 9:10 AM
  • User1899595498 posted

    Can gmail free of charge account is used to send an email like in your example?

    Can you show the setting "web.config" file?

    Thanks.

    Monday, June 25, 2012 9:07 AM
  • User1320101480 posted

    Yes you can use it.With this code, you don't have to put any other details in web.config.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 25, 2012 12:11 PM
  • User-887428016 posted

    Hi Saurabh

    I'm pretty new to asp.net and I need to use the "sending email with WebService"....

    Whats the diference writing the code above in the asmx or in the aspx.cs??

    Thanks!

    Tuesday, February 19, 2013 3:51 PM
  • User1320101480 posted

    Hi Saurabh

    I'm pretty new to asp.net and I need to use the "sending email with WebService"....

    Whats the diference writing the code above in the asmx or in the aspx.cs??

    Thanks!

    You can access web services from other applications i.e. for application communication and can configure the way it communicates with others(the protocols / Data format).

    In aspx i.e. web forms it's used for creating a website that people would be accessing and can respond to events.

    Wednesday, April 17, 2013 2:29 PM