c# - Send email from webservice -


itemprop = "text">

I have a method to send mail from C #:

  public static void SendEmailWebService {String asunto, String body, string} {Try {// MailMessage mm = New MailMessage {takes config from web.config = new MailAddress (@ "no-reply@site.com", @ "Notificacion") To = {(to)} New MailAddress, topic = asunto, IsBodyHtml = true, physical = body, HeadersEncoding = System.Text.Encoding.UTF8, SubjectEncoding = System.Text.Encoding.UTF8, BodyEncoding = System .Text.Encoding.UTF8}; Smtp.Send (mm); } Hold (Exception Pre) {// Error}}  

If I call this method normaly, it works, but if I say it from a webservice then it fails .

In Kathak, the exception is:

Authentication is required when trying to send this mail server to a non-local email address. Please check your mail client settings or contact your administrator to confirm that the domain or address is defined for this server.

I do not know why it is not worcing when I tell it from the webservice.

The error message is saying that your mail server requires authentication while thereby achieving this goal There are several ways to do this, you can create a new NetworkCredential object and that you can supply for your parameter named MailMessage:

  public static void SendEmailWebService (for string asunto, string body, string) {try {// MailMessage mm = New MailMessage {takes config from web.config = new mailAddress (@ "no-reply@site.com", @ " Notificacion "), to {{New MailAddress (to)}, subject = asunto, IsBodyHtml = true, physical = body, HeadersEncoding = System.Text.Encoding.UTF8, SubjectEncoding = System.Text.Encoding.UTF8 , BodyEncoding = System.Text.Encoding.UTF8, UseDefaultCredentials = false, Credential = New network credentials ("username", "password")}; Smtp.Send (mm); } If you do not want to add your code to  network credentials , you can configure it from Web.config file 

  & lt; System.net & gt; & Lt; MailSettings & gt; & Lt; Smtp & gt; & Lt; Network host = "" port = "" user name = "" password = "" /> & Lt; / Smtp & gt; & Lt; / MailSettings & gt; & Lt; /system.net>  

Comments