简体   繁体   中英

Email not working in Hosted Application

Email send functionality is not working in hosted application. But it is working on local machine. Code is

[WebMethod]
    public static string SendMail(string To,string Subj,string Body)
    {

        MailMessage emailmsg = new MailMessage();
        emailmsg.From = new MailAddress("salam@rainhopes.com");
        emailmsg.To.Add(To);
        emailmsg.Subject = Subj;
        emailmsg.Body = Body;
        emailmsg.IsBodyHtml = true;

        //SMTP SERVER DETAILS
        SmtpClient smtpc = new SmtpClient("smtp.gmail.com");
        smtpc.Port = 587;
        smtpc.UseDefaultCredentials = false;
        smtpc.EnableSsl = true;
        smtpc.Credentials = new NetworkCredential("salam@rainhopes.com", "******");
        smtpc.Send(emailmsg);
        return " your email send sucessfuly!.. check your email";
    }

Please give me any solution

Exception detail is

"Message":"Request for the permission of type \u0027System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0027 failed.","StackTrace":"   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark\u0026 stackMark, Boolean isPermSet)\r\n   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark\u0026 stackMark)\r\n   at System.Security.CodeAccessPermission.Demand()\r\n   at System.Net.Mail.SmtpClient.set_Port(Int32 value)\r\n   at Flair.sendmail.SendMail(String To, String Subj, String Body)","ExceptionType":"System.Security.SecurityException"

in web.config under <system.web> add <customerrors mode="Off"> to see full exception

 <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true"/>
    <authentication mode="None"/>
  </system.web>
  </configuration>

Check your SMTP configuration,for gmail it is :

 <system.net>
    <mailSettings>
      <smtp>
       <network host="smtp.gmail.com" port="587" userName="eran@thereturnvalue.com" password="111111" defaultCredentials="false" />
      </smtp>
    </mailSettings>
  </system.net>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM