Thursday, June 18, 2015

Send Mail Using SMTP,With UserName- Password ,#C

 private string SendEmailSMTP(string EmailId, string Subject, string MessageBody)
    {
        string Confirmation = string.Empty;
        try
        {

            string Subject = Subject; //----Subject of the Mail
            string MessageBody = "MessageBody"//--Your Message Body Text Goes Here";
         
            MailMessage Mail = new MailMessage();
            MailAddress fromAddress = new MailAddress("FromAddress@someexample.Com");
         
            Mail.From = fromAddress;
            Mail.To.Add(EmailId);//-----To Mail Address
            Mail.Subject = Subject;
            Mail.Body = MessageBody;
            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Host = "00.00.0.000"; //---Host Address Goes Here
         
            SmtpServer.Port = 25;
            SmtpServer.Credentials = new NetworkCredential("YourUserName", "YourPassword");
            Mail.IsBodyHtml = true;        
            SmtpServer.Send(Mail);
            Confirmation = "1";
        }
        catch
        {
            Confirmation = "0";
        }
        return Confirmation;
    }

Sending Rest API Request from C#

public Object FTRJMSRequest(string RequestType, string Type, string SubType, string SubSubType, string QRCType, string FaultFound, string ResolutionType, string Mobile)
 {
 const string URL = "http://00.00.000.00:8080/knowmaxservice/webresources/webservice/SendRequest/";

 string DATA = "{'RequestType':'" + RequestType + "','Type':'" + Type + "','SubType':'" + SubType + "','SubSubType':'" + SubSubType + "','QRCType':'" + QRCType + "','FaultCode':'" + FaultFound + "','ResolutionCode':'" + ResolutionType + "','Mobile':'" + Mobile + "','Notes':'Testing in Progress....','UserName':'Service_Setup'}";

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
 request.Method = "POST";
 request.ContentType = "application/json";
 request.ContentLength = DATA.Length;
 using (Stream webStream = request.GetRequestStream())
 using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII)) {
 requestWriter.Write(DATA);
 }
 try {
 WebResponse webResponse = request.GetResponse();
 using (Stream webStream = webResponse.GetResponseStream())
 {
 if (webStream != null) {
 using (StreamReader responseReader = new StreamReader(webStream))
 {
           string response = responseReader.ReadToEnd();
                    objReturn = response.ToString();
 //Console.Out.WriteLine(response.ToString());
 }
 }
}
}
 catch (Exception e)
 {
 //Console.Out.WriteLine("-----------------");
 //Console.Out.WriteLine(e.Message);
 }
 return objReturn;
}