Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

send mail from app.net

Posted on 2016-10-21
8
Medium Priority
?
62 Views
Last Modified: 2016-10-21
i have web App
i need send email after SAVE in the database

I'm using a code that uses my email and password, is there any way to use a smtp unauthenticated

this code in web.config
    <add key="MailUser" value="elopezh@smi.com.pe" />
    <add key="MailUserPass" value="inicio01" />
    <add key="MailServer" value="smtp.office365.com" />
    <add key="MailServerPort" value="587" />
    <add key="MailServerSSL" value="1" />


--- this is the class
public static bool EnviarCorreo(MailModel correo)
        {
            try
            {
                Log.WriteLogInfo("Inicio Enviar correo");
 
                //tomar los datos de conexion del webconfig
                string flgenviarcorreo = System.Configuration.ConfigurationManager.AppSettings["FlgEnviarCorreo"];
 
                string mailuser = System.Configuration.ConfigurationManager.AppSettings["MailUser"];
                string mailuserpass = System.Configuration.ConfigurationManager.AppSettings["MailUserPass"];
                string mailserver = System.Configuration.ConfigurationManager.AppSettings["MailServer"];
                string mailserverport = System.Configuration.ConfigurationManager.AppSettings["MailServerPort"];
                string mailserverssl = System.Configuration.ConfigurationManager.AppSettings["MailServerSSL"];
 
                MailMessage mail = new MailMessage();
                mail.IsBodyHtml = true;
                mail.From = new MailAddress(mailuser);
 
                mail.Subject = correo.Asunto;
                mail.Body = correo.MensajeHtml;
                mail.Priority = MailPriority.High;
 
 
                string palinBody = correo.MensajeTexto;
                AlternateView plainView = AlternateView.CreateAlternateViewFromString(palinBody, null, "text/plain");
 
                // then we create the Html part to embed images,
                // we need to use the prefix 'cid' in the img src value
                string htmlBody = correo.MensajeHtml;
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
                    htmlBody, null, "text/html");
 
                // add the views
                mail.AlternateViews.Add(plainView);
                mail.AlternateViews.Add(htmlView);
 
                SmtpClient clienteenvio = new SmtpClient();
                clienteenvio.UseDefaultCredentials = false;
                clienteenvio.DeliveryMethod = SmtpDeliveryMethod.Network;
 
                clienteenvio.Credentials = new NetworkCredential(mailuser, mailuserpass);
                clienteenvio.Host = mailserver;
                clienteenvio.Port = Convert.ToInt32(mailserverport);
                clienteenvio.EnableSsl = false;
                if (mailserverssl == "1")
                {
                    clienteenvio.EnableSsl = true;
                }
 
                try
                {
                    clienteenvio.Send(mail);
                    Log.WriteLogInfo("Se envio el mensaje de correo " + mail.To[0].Address);
                    return true;
                }
                catch (Exception ex)
                {
                    Log.WriteLogError(ex.Message);
                    return false;
                }
            }
            catch (Exception ex)
            {
                Log.WriteLogError(ex.Message);
                throw ex;
            }
        }


in App web
            if (DAL_Solicitud.Guardar(ref solicitud))
            {

                MailModel resul = new MailModel();
                resul.Para.Add("elopezh@smi.com.pe");
                if (model.emp_inv_des == "10359") resul.CC.Add("pchanca@smi.com.pe");
                resul.MensajeTexto = "Prueba de envio de correo: MensajeTexto";
                resul.MensajeHtml = "<table>" +
                "<tr><td style='font-weight:bold'>Proyecto:" + "</td><td>" + solicitud.proyecto + "</td>" + "</tr>" +
                "<tr><td style='font-weight:bold'>Solicita:" + "</td><td>" + solicitud.emp_comercial + "</td>" + "</tr>" +
                "<tr><td style='font-weight:bold'>Empresa:" + "</td><td>" + solicitud.empresa + "</td>" + "</tr>" +
                "</table>";
                resul.Asunto = "Solicitud de Proyecto";
                VTRUtil.EnviarCorreo(resul);

                return Json(new { coderror = solicitud.coderror, msgerror = solicitud.msgerror });
            }
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 2000 total points
ID: 41853855
If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously. You have UseDefaultCredentials set to false, so just do not specify any credentials and try sending it that way...
0
 

Author Comment

by:enrique_aeo
ID: 41853905
already you have that code

SmtpClient clienteenvio = new SmtpClient();
clienteenvio.UseDefaultCredentials = false;
clienteenvio.DeliveryMethod = SmtpDeliveryMethod.Network;
0
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 41853907
did you comment out the line below?

clienteenvio.Credentials = new NetworkCredential(mailuser, mailuserpass);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:enrique_aeo
ID: 41854481
it is comment, but not send mail
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41854489
do you get any error?
0
 

Author Comment

by:enrique_aeo
ID: 41854500
THIS IS THE ERROR
Message      "Server does not support secure connections."      string
Source      "System"      string
StackTrace      "   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)\r\n   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)\r\n   at System.Net.Mail.SmtpClient.GetConnection()\r\n   at System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n   at srvvtr.VTRUtil.EnviarCorreo(MailModel correo) in

WEB.CONFIG
<!--Configuracion de Correo-->
    <add key="FlgEnviarCorreo" value="1" />
    <add key="MailUser" value="sistemas@smi.com.pe" />
    <add key="MailUserPass" value="Pa$$w0rd02" />
    <add key="MailServer" value="smtp.smi.com.pe" />
    <add key="MailServerPort" value="25" />
    <add key="MailServerSSL" value="1" />
0
 

Author Comment

by:enrique_aeo
ID: 41854540
this is the problema
 <add key="MailServerSSL" value="1" />

i change  <add key="MailServerSSL" value="0" />
0
 

Author Closing Comment

by:enrique_aeo
ID: 41854543
<add key="MailServerPort" value="25" />
    <add key="MailServerSSL" value="0" />
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question